What is the MongoDB Data Modeling?

MongoDB stores data in a flexible schema and not like the RDBMS tables in which first we need to define the structure of the table and then we put the data inside that. MongoDB collections don't require a document structure. In a MongoDB collection, a document can have a different set of fields, a data type that can be different from other documents in the same collection. If there is a requirement to change the structure of a document in a collection such as adding a field, removing fields, or changing the field value then we can update the document to a new structure.

Let us understand the MongoDB data modeling with the following example. We have two relational tables Employee Detail and Employee Car Detail.

The data in relational databases data will look like below in the left table format. If we create the MongoDB document based on these two tables for Emp_name then the MongoDB JSON document will look below the right format.

Mongodb architecture


MongoDB Data Model Design

MongoDB Data Modeling represents the designing of the structure of documents and that represents the relationship of an application between data. MongoDB provides two types of Data Modeling namely the Embedded data model and the Normalized data model. We can choose anyone the data model while creating a document.

Let us see each MongoDB data model in the following section.

1. Embedded Data Model

In Embedded Data Model we can insert the related data in a single document structure. It is also called a "denormalized" model which takes the benefits of MongoDB's rich documents. It provides a fast read in a single database operation also updates the related records in a single write operation.

We can use Embedded Data Model when:

  • When we have the "contains" relationships in between the data.
  • When there are one-to-many relationships between the data.

Let's suppose, we have employee detail that has the Personal_details, Address and Contact detail in different MongoDB documents. In an Embedded model, we can embed all these documents in a single document as mentioned below.

Mongodb embedded data model

2. Normalized Data Model

In the Normalized Data Model, the relationship between the documents is defined through reference.

We can use Normalized Data Model when:

  • When the performance of the embedded model is not sufficient and in result, duplicate data is coming.
  • When there is the requirement of many to many relationships.
  • When there is a need for hierarchical larger data sets.
Mongodb normalized data model


Schema Design Consideration In MongoDB

The following points can be considered while designing the schema in MongoDB.

  • Based on the data and user requirements the schema should be designed.
  • If the objects are being used together then those objects should be put together.
  • Perform the joining operation only when we are writing data and not on reading.
  • Avoid duplicates in the data.
  • Perform the complex aggregation in the schema.