MongoDB schema design best practices

Geek developer in web technologies, open-source contributor, and content writer. I share my learnings on twitter and blogs, follow me for latest content.
Search for a command to run...

Geek developer in web technologies, open-source contributor, and content writer. I share my learnings on twitter and blogs, follow me for latest content.
This was simple and straightforward. Thanks for sharing.
Amazing article Vasanti Suthar I definitely learned a few new things about MongoDB.
Authentication is a fundamental aspect of modern web and mobile applications. It ensures that users can securely access an app while protecting their data. Firebase, a platform developed by Google, offers a simple and efficient way to add authenticat...

Hello there! My name is Shivam, and today, I will show you how to create this beautiful horizontal scrolling parallax effect with HTML, CSS, and JavaScript. Wouldn't this look stunning on your blog or portfolio site? To achieve this effect, we will ...

Dealing with fonts can get quite overwhelming. For quite some time, I was searching for a way to self-host google fonts because the google fonts API's network request increased the render-blocking time more than I expected. I stumbled upon fontsource...

Our favorite month is here! And we have some amazing activities planned for our community 🤩 Yes, you are right. We are talking about Hacktoberfest! In this blog, we want to cover various ways you can participate in Hacktoberfest through the ReactPla...

The second edition of ReactPlay Hackathon is here!

MongoDB has been a popular database among all NoSQL databases used these days for storing big data. Unlike relational databases, it may not provide techniques for defining good relationships but it provides flexibility in defining the schema of our collection, which is one of the core advantages of the platform.
Flexibility here is:
A well-defined schema is very much important in MongoDB to query the data more efficiently with less hustle and improve the scalability as well. Unwanted schema objects may not impact your database at earlier stages but with the increasing amount of data, querying can slow down the performance and will acquire more memory.
Thus, keeping this in mind, a good schema design practice is needed. This article will help you understand the theoretical core concepts of MongoDB that can be used to improve scalability and performance along with the types of schema that can be defined.
MongoDB is a document-oriented NoSQL database that can store large amounts of data in a JSON format in a collection and has a dynamic schema. It has a faster execution than RDBMS.
Now, while defining schema across multiple databases most devs think that the approaches are similar, we store the data and then apply queries to fetch the same, well this is not how it works, schema is designed according to how we query the data and the functionality provided by the databases. You must first know how your application wants the data, for that let's first look into the approaches between Relational databases and MongoDB databases.
Relational databases provide a good grasp of defining relationships which helps devs in only focusing on the data they have while building schema. The relationship is built by normalizing the data into multiple tables to remove redundancy and any data can be fetched according to the query we provide i.e using join. Look into the example below.

Data is normalized into tables and are joined using the foreign key concepts.
MongoDB data modeling depends upon the data that your application needs. Following are the key things that need to be considered while building a schema
In MongoDB, storing the data is directly proportional to querying the data, the way you keep is the same as the way you efficiently query for your application and that will improve the performance as well. Look into the example below.

In MongoDB there is no normalization, the data is modeled in JSON format as we want, the child data is stored using an embedded document, or reference to the child table is stored using lookup. These are the two types through which data can be stored.
Embedded documents are documents nested inside another document, also called nested documents within the collection.
There is another approach alternative to Embedding, if the document size is quite large it's good to create a separate collection and give its reference to the parent collection’s document using a unique id and connecting through the $lookup operator that works similarly to the JOIN operator.
Relationships represent how the multiple documents are logically connected in MongoDB to create a more manageable database. This is not such as normalizing into tables but creating through embedded and referenced relationships. You can set up the relationship based on the need for your data and the performance of the query. Let’s look into the types of relationships
One-to-one is the most fundamental relationship to define. One parent key with one embedded child document creates a 1:1 relationship. For example, one person can have only one passport.

Here, the user’s passport details are embedded in the passport key, there is no need to create a separate collection until and unless the data is quite large.
A one-to-many relationship is having a one-key of parent document with embedded or referenced child documents, creating a 1:N relationship. Suppose, a person can have multiple cars, for this, we can create relationships using embedding or referencing.

Here, the child data is embedded in the parent key ‘car’, if you want to get the data from the name of the person, then embedding would be better to use, as you only have to provide a single query to get the child data as well.
Referencing on the other side can be used to normalize the large data, creating a separate collection and referencing them would be a better fit for the query.
One-to-few relationships are used to connect the parent data with a few child data. Now, a single user can have multiple addresses, to store those few documents embedding relationships should be used for query performance.

Now suppose you have millions of child records such as tweet replies, probably they would get larger and larger, for this we neither embed child documents nor store the arrays of referenced collection object Id, as it will be out of bound. The only preferred way will be to have a document of reply, then store the objectId of reply in the documents of the tweet. Look below for the example:

Now, that we have seen the single parent and different types of child documents, let’s look into the N: N relationship. Take an example of events, where a user can be invited to multiple events and an event can have multiple users.


Here, in both the collections, there is a subarray of objectId, creating many-many relationships.
The most fundamental aspect of Mongodb is query pattern, very much important to know how your application needs your data to effectively get the data with better performance.
Profiling is a kind of debugging for MongoDB performance, you will get to know the issue and track its slow-running queries or transactions. Some profiling tools are MongoDump, MongoSkin, and Persona Monitoring and Management.
MongoDB provides you to create a dynamic schema, but it would be beneficial if you create the schema precisely and accurately according to the needs of your data. It would be very costly if you don’t define your schema well enough earlier and you have to make the changes afterward down the road. Indexing in MongoDB will mostly be used for searching from the documents, instead of querying the whole document, indexes are created so that it gets easy to search from a few fields.
In relationships, embed the child data whenever possible, it will be beneficial for query performance as we don’t need to pass multiple queries to get from another collection. Embedding will improve the performance with the help of a single query.
Getting data from other related collections that store a large amount of data will be a query with the $lookup operator.
Ensuring these relationships will help you to get fruitful results and can easily handle the data.
MongoDB stores data into files of around 64MB or up to 2GB in size. These files are divided logically into different blocks stored in the virtual memory blocks. The data from virtual blocks are loaded into the memory map, where it leverages the system cache. These files are the data that your application is constantly requesting, these are called ‘Working sets’ in MongoDB. If the working set doesn’t fit in the memory, then the OS will need to swap one part of the Working set to disk, to access another part of it.
So if the working set exceeds the size of RAM, then either increase RAM or shard the MongoDB system, it will split the ‘Working set’ to multiple RAMs.
Horizontal scaling is an approach to setting up multiple MongoDB machines to increase performance and scalability. As the data grow with the application it will be not possible to handle one database server with data accessibility, instead, MongoDB will store the records in multiple MongoDB machines keeping them in sync with automatic data failover in case the server fails.
Replication is to create multiple replicas of the server so that if one server fails, you will have a copy of the database on another server. For replicas, the entire data volume must be stored in a single server, so that we can get a 100% copy of data in case of server failure.
The 'Schema design best practices' can be defined as an effective solution to the problem of structuring the data in a database and making it consistent by avoiding redundancy and preserving data integrity.
Designing the right schema model seems quite tricky, where various concepts and terms such as ACID vs BASE vs BASEPROPERTIES, dynamic schema, and how to handle the different scenarios of schema design to build a robust production system. All that information seems confusing at first glance, but I've organized them as best practices in my article that explains what you need to know about MongoDB schema design.