MongoDB – Model One-to-One, One-to-Many Relationships Embedded Documents | SpringBoot

With MongoDB, we can structure related data by embedded documents. In general, embedding gives a better performance for read operations. So in the tutorial, JavaSampleApproach will show you way to work with Embedded Documents using SpringBoot.

Related posts:
Spring JPA One to Many Relationship
SpringData MongoRepository
SpringData MongoDB GridFsTemplate to save, retrieve, delete binary files (Image, Text files)

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6RELEASE
– MongoDB: v3.4.1

II. MongoDB – Embedded Documents

Embedded Documents are generally known as denormalized models. It is a way to structure related data in a single document. See below diagram:

mongodb embedded document - structure

In general, we decide to design data as embedded models in case: data contains one-to-one or one-to-many relationships between entities.

With embedding data model, in general, it gives us a better performance for read operations. And we can use a single atomic read/write database operation to request and retrieve or update related data.

However, a weakness point is embedding related data in documents may lead to situations where documents grow after creation.

III. Practice

In the tutorial, we use SpringToolSuite to create a SpringBoot project for MongoDB embedded documents:

mongodb embedded document - springboot project structure

Step to do:
– Create SpringBoot project
– Create Document models
– Create MongoRepository
– Run and check results
– Implement Client

1. Create SpringBoot project

Using Spring Tool Suite, create a SpringBoot project. Then open pom.xml file, add dependencies:

2. Create Document models

Create 3 class models {Company, Product, Contact} with one-to-one relationship (between Company & Contact),
and one-to-many relationship (between Company & Product) in embedded data model structure.

Company

Contact:

Product:

3. Create MongoRepository

interface CompanyRepository extends MongoRepository

– Open application.properties, add configuration to connect with Mongo Server:

4. Implement Client

Use CommandLineRunner to implement a simple client to manipulate data from MongoDB via CompanyRepository:

5. Run and check results

Start MongoDB server by commandline: .\MongoDB\Server\3.4\bin>mongod.exe

-> Logs:

Build and Run the SpringBoot project with commandlines: mvn clean install, mvn spring-boot:run

-> Logs:

Run MongoDB shell by commandline: .\MongoDB\Server\3.4\bin>mongo.exe:

-> Logs:

Results with MongoDB shell:

mongodb embedded document - mongodb shell results

show dbs is used to show all MongoDB databases. -> Having one database jsa_mongodb which is created by the SpringBoot MongoRepository application.
use jsa_mongodb is used to select the jsa_mongodb database to work.
show collections is used to show all collections in current database. -> We just have one collection: {company}
db.company.find() is used to query all documents of the collection company. -> We have 2 Company documents {Apple, Samsung}.

IV. Sourcecode

SpringMongoDbEmbeddedDocument


Related Posts


3 thoughts on “MongoDB – Model One-to-One, One-to-Many Relationships Embedded Documents | SpringBoot”

  1. your demo example for one to many relation in mongodb works fine.but in company to address mapping only company collection data is inserted corresponding address collection is empty.

    1. More infomation for right understanding about MongoDB Emdedded model:

      – Emdedded model is the way we embed related data in a single structure or document.

      => So we just have one collection that store document.
      In the case, We have only company collection. And address collection does not exist.

      Regards,

Got Something To Say:

Your email address will not be published. Required fields are marked *

*