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)
Angular 4 + Spring Boot + MongoDB CRUD example

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.




3. Create MongoRepository

interface CompanyRepository extends MongoRepository

– Open, 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} is used to query all documents of the collection company. -> We have 2 Company documents {Apple, Samsung}.

IV. Sourcecode


By grokonez | August 18, 2017.

Last updated on November 28, 2017.

Related Posts

8 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 stores document.
      In the case, We have only company collection. And address collection does not exist.


  2. Hey dude, really nice your demo!

    I just have a silly question,

    I have two entities: Plant and Plantation. Plant is a document that contains all plants I have (Tomato, Potato, etc), and plantation is another document that contains a Plant and details about plantation (When I sown it).
    I know my thinking is still in ER DB but I cannot understand how I could reuse those plants inside my plantation without create a new/duplicated plant for every single plantation.

    I saw people using a plantId inside plantation and just keep the relationship like a string to another document. But in code perspective it is not nice, because when I am gonna work with my plantation object I will not have my plant as well, only plant ID.

    How could I do that?

    Thank you!

Got Something To Say:

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