How to Model your data in MongoDB
MongoDB is a database that is also known as a NoSQL database. In another word, MongoDB is a schema-based database that is used to store data in documents.
It is highly scalable and powerful. It stores data by following a field-value data structure that is most likely JSON but in MongoDB, it is known as BSON.
It is a great database system and you may build many types of modern, scalable, and flexible web applications by using it.
As you can see, it is very powerful but to use it in the most efficient way you need to know how to model data.
In this tutorial, you are going to learn about how to model data in MongoDB and at the end of this tutorial, you will accomplish a clear concept of data modeling and designing in MongoDB.
Prerequisite
Most of the things will be presented visually or using diagrams in this tutorial but to complete this tutorial in the most effective way it will be nice if you have these things before
- A configured computer with an internet connection.
- Basic knowledge of creating data Models in MongoDB.
- A text editor – You can choose any text editor on which you may write JavaScript code such as sublime text, nodepad++, etc. In this tutorial, we will use visual studio code.
- Nice to have little knowledge about what is embedding and referencing in MongoDB, if you don’t, not need to worry we will cover it later in this tutorial.
Topics To Be Covered In This Tutorial
- Introduction of data modeling in MongoDB
- Different types of relationships between data
- One To One relationship
- One To Many relationship
- One To Few
- One To Many
- One To a Lot
- Many To Many relationship
- Methods to create MongoDB relationships
- Referenced relationships
- Embedded relationships
- Conclusion
Introduction of data modeling in MongoDB
Basically, Data Modeling is a process of taking unstructured data that is generated from the real world scenario and then structuring it into a logical data model in a database. This thing has been done under a set of criteria. For example, if you want to design an online shop data model then at the beginning there will be tons of unstructured data that you need to use. But to use it properly you need to organize them in a structured way so that you can use them properly.
The process of doing it is known as data modeling. This data modeling is the most demanding and essential part of the development and the entire application depends on it. We will explore things more elaborately in the following section of this tutorial.
Different types of relationships between data
There are different kinds of relationship that exists between data based on different databases. In MongoDB data modeling, there are three different types of relationships between data are exist. These are –
- One To One
- One To Many
- Many To Many
You can already understand by their names what they mean. In one to one relationship, there will be one value for one field, on the other hand, there will be multiple values for one field in one-to-many relationships and finally, in the many-to-many relationship, there will be multiple values for multiple fields. This is a brief intro about what are the different types of relationships between data. We will explore them in detail in the following section of this tutorial.
One To One
In the MongoDB data model, when one field can only have one value it will consider as a one-to-one relationship between data. This is the simplest data relationship model. For example, you are working with a student management system and you have two entity that is student and identifier.
There’s a one-to-one relationship between these two entities. Because a student may have only one unique id. He can not consume multiple id on the other hand multiple id can not be the same for one student. Let’s see the below diagram to get a clear understanding of it.
Here, you can see that one student has only one identifier, and one identifier is applicable for only one student. This kind of relationship between data is known as a one-to-one relationship.
One To Many
A one-to-many relationship is the most important and most used data modeling relationship in MongoDB. This is also divided into three parts and these are-
- One To Few
- One To Many
- One To a Lot
These differences are being created on the basis of the relative amount of many. Let’s see an example of a one-to-few relationship. To demonstrate it, we may consider it as a drama and a drama may have won some awards but not many.
The second type is one-to-many relationships. It refers that one document may have a relationship with more than hundreds or even thousands of other documents. For example, a drama may have thousands of reviews. In that case, it will no longer to one-to-few relationships.
There is a bit more complex to explain the one-to-lot relationship but to demonstrate it, you may compare it with logs. I assume that you are familiar with logs. If we try to see logs of our drama application program then it may return millions of logs.
These are three relationships of one-to-many and among these types, one-to-many relationship data modeling is the most important and widely used.
Many To Many
In this type of relationship, there can be relationships in both ways like one drama can have many actors, on the other hand, many actors can do one drama. Moreover, many dramas may have many actors and many actors may do many dramas.
It is a kind of vice-versa relationship between the documents. These are the different types of relationships in data modeling. Among them, one-to-few is the most important one, and in a NoSQL database like MongoDB, one-to-many relationships types are also important because referencing and embedding are dependent on them, and in the following section we will learn about referencing and embedding in MongoDB data modeling.
Methods to create MongoDB relationships
The most important aspect that we need to learn in the MongoDB database is the relationship. In MongoDB, you have the following two methods for creating a relationship.
- Referenced relationships
- Embedded relationships
To create a relationship of a BSON document you have to choose either referenced relationships or embedded relationships. We will explore them in detail in the following section.
Referenced relationships
The referenced relationship is also known as the Normalized form. In the referenced form, all the related documents are kept separated. For example, we have one article that has multiple comments.
Here, you can see that one article has two comments. It can be more but for example purpose, we are demonstrating two. These documents are separate from each other. Now how can you know which comment belongs to which article? Here, referencing comes to play its role.
If you notice you can see that, we have kept the comment_ID from each comment document, instead of keeping the whole document.
This type of approach is known as child references. The main reason behind it is that the article document is considered as a parent and the comment documents are considered as child and they have a parent-child relationship between them.
In a relational database, this kind of reference is always followed but in a NoSQL database like MongoDB, there is another approach named embedded relationships that has been followed. Normalized data is very useful when an application is needed to access or query the separate document from the database.
Embedded relationships
This embedded relationship is also known as the denormalized form of data where documents are simply denormalized by embedding the data into the main document. See the below example:
Here, you can see that our main document article and the embedded documents are comments. Where we do not need any separate collections or ID. In the embedded relationships of data modeling, all the related data are embedded into the main document.
As a result, the application may need fewer queries to the database as we can get all the information in one query. On the other hand, it is impossible to query embedded documents on their own. As a result, if we need to access the embedded documents then it will not be a wise decision to choose this data model.
Conclusion
This tutorial is mainly focused on the theoretical aspects of data modeling in MongoDB. But this is the most important and essential knowledge that every backend developer should know. In this whole tutorial, we have covered the data modeling, different relationships between data, methods of data modeling with their pros and cons.
You can write code or write a solution but how efficient your solution is mattered the most. Working in a large application one decision may make the program successful or broken. This tutorial will help you to understand the core things about data modeling in MongoDB and increase your ability to think properly and make a perfect decision in the long run.
All you need to do is to take this tutorial as a blueprint of data modeling, and, gather the knowledge about these kinds of stuff and finally implement it in the real world scenario. If you want to know more things about data modeling you may visit the official website of MongoDB.