MongoDB Data Model Designs
For modeling data in MongoDB, two strategies are available. These strategies are different and it is recommended to analyze scenario for a better flow.
The two methods for data model design in MongoDB are:
- Embedded Data Model
- Normalized Data Model
1. Embedded Data Model
This method, also known as the de-normalized data model, allows you embedd all of the related documents in a single document.
These nested documents are also called sub-documents.
Embedded Data Model example
If we obtain student information in three different documents, Personal_details, Contact, and Address, we can embed all three in a single one, as shown below.
{
_id: ,
Std_ID: "987STD001"
Personal_details:{
First_Name: "Rashmika",
Last_Name: "Sharma",
Date_Of_Birth: "1999-08-26"
},
Contact: {
e-mail: "rashmika_sharma.123@gmail.com",
phone: "9987645673"
},
Address: {
city: "Karnataka",
Area: "BTM2ndStage",
State: "Bengaluru"
}
}
2. Normalized Data Model
In a normalized data model, object references are used to express the relationships between documents and data objects. Because this approach reduces data duplication, it is relatively simple to document many-to-many relationships without having to repeat content.
Normalized data models are the most effective technique to model large hierarchical data with cross-collection relationships.
Normalized Data Model Example
Here we have created multiple collections for storing students data which are linked with _id.
Student:
{
_id: <StudentId101>,
Std_ID: "10025AE336"
}
Personal_Details:
{
_id: <StudentId102>,
stdDocID: " StudentId101",
First_Name: "Rashmika",
Last_Name: "Sharma",
Date_Of_Birth: "1999-08-26"
}
Contact:
{
_id: <StudentId103>,
stdDocID: " StudentId101",
e-mail: "rashmika_sharma.123@gmail.com",
phone: "9987645673"
}
Address:
{
_id: <StudentId104>,
stdDocID: " StudentId101",
city: "Karnataka",
Area: "BTM2ndStage",
State: "Bengaluru"
}
Data Modelling in MongoDB
Data modeling in MongoDB is the process of designing and creating the structure of collections and documents that will be stored in the database.
Maintaining data in an organized manner is very important for database efficiency. It also ensures data security, data accuracy, and better functioning. To maintain an organized database, it is important to learn data modeling.
In this article, we will go through MongoDB data modeling with examples and explore MongoDB’s features and capabilities.
Contact Us