MongoDB Data Modeling
A Comprehensive Guide for beginners
MongoDB is a document-oriented cross-platform database that makes storing and retrieving complex data easy and fast. It uses a JSON-like structure for the documents, which is familiar to anyone working with modern applications.
MongoDB document schema
Data in MongoDB has a flexible schema document in the same collection. Unlike SQL databases, where you must determine and declare a table’s schema before inserting data, In MongoDB’s collections, by default, do not require their documents to have the same schema.
In MongoDB Collections,
- The documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection.
- To change the structure of the documents in a collection, such as add new fields, remove existing fields, or change the field values to a new type, update the documents to the new structure.
Types of Data Modeling in MongoDB
The data model in MongoDB is basically divided into two types:
(i) Embedded / Denormalized data model
(ii) Referenced / Normalized data model
(i) Embedded / Denormalized data model
The embedded data model also known as the de-normalized data model is a data model where all the related data are embedded in a single document.
In this model, the document is like joining one or more related documents into a single document. This model is very useful when you need to retrieve all the related data.
This data model allows for the retrieval of all the related data from a single document for a single database query.
Example
Suppose an Employee document like as follows,
{
"_id" : "1",
"employeeId": "101",
"Name" : "Smith",
"Age" : 19,
"Income" : 2000.0,
"Address" : {
"Street" : "Dhanmondi 27",
"Zip" : "7002",
"City" : "Dhaka",
"CountryCode" : "BD"
},
"Communications" : [
{
"Channel" : "Mobile",
"Value" : "+123-456-789"
},
{
"Channel" : "Email",
"Value" : "abc@xyz.com"
}
]
}
Advantages of Embedded data model
As we can read all the related data in a single query or operation, it gives us better performance.
Disadvantages of Embedded data model
As all the related data are embedded in a single document, there has been a change in data duplications.
We need to be careful about the document size when data is stored because the maximum BSON document size is 16 megabytes in MongoDB.
(ii) Referenced / Normalized data model
The reference data model also known as the normalized data model is a data model where related data are stored in different collections. In this model, a reference of data in one collection is used to connect the other related data in different collections.
In this model, there consist of one-to-one or one-to-many relationships between the documents.
Example
The Employee collection will be as follows,
{
"_id" : "1",
"employeeId": "101",
"Name" : "Smith",
"Age" : 19,
"Income" : 2000.0
}
The Address collection will be as follows,
{ "_id" : "5",
"employeeId": "101",
"Street" : "Dhanmondi 27",
"Zip" : "7002",
"City" : "Dhaka",
"CountryCode" : "BD"
}
The Communications collection will be as follows,
{ "_id" : "3",
"employeeId": "101",
"Channel" : "Mobile",
"Value" : "+123-456-789"
}
{ "_id" : "4",
"employeeId": "101",
"Channel" : "Email",
"Value" : "abc@xyz.com"
}
Advantages of the Referenced data model
As the related documents are stored in separate collections, there is less possibility of data duplications.
We can implement more complex many-to-many relationships of collections and also can represent the data in a hierarchical model.
Disadvantages of the Referenced data model
As all the related data are stored in separate collections, to get all the related data, we need to perform more operations to get that data.
Also, we need to either need multiple databases or to join multiple collections to get those data.
Summary
When designing a data model, consider how applications will use your database. If your application needs are mainly read operations to a collection, adding indexes improves performance. To know detail about MongoDB Indexing read this article.
Happy reading!!