MongoDB Sharding Tutorial with Example

All That You Need To Know About Sharding in MongoDB:

We learned about Regular Expression in MongoDB in our previous tutorial.

In this tutorial, we will discuss Sharding in MongoDB.

Explore all the tutorials in this MongoDB Online Tutorial series for a perfect understanding of the concept.


Sharding in MongoDB

Sharding is the architecture to store big data in distributed servers.

In MongoDB, sharding maintains a huge data and is mostly used for massively growing space requirement. Now big applications are based on the end to end transactional data, which is growing day by day and the requirement of space is rapidly increasing.

Just because of the increase in information storage, a single machine is not able to deal with the huge storage capacity. We have to share the information in chunks between different servers.

In mongo, sharding provides horizontal scale-up application architecture by which we can divide information upon different servers.

With the help of sharding, we can connect multiple servers with the current instance of the database to support growing information easily. This architecture maintains a load of information automatically upon connected servers.

A single shard represents as a single instance of the database and collectively it becomes a logical database. As much the cluster grows-up with a combination of the different shard, accordingly the responsibility of each shard becomes lesser.

For Example, we have to store 1GB of information in MongoDB. In the Sharding architecture, if we have four shards, then each will hold 250MB and if we have two shards then each will hold 512MB.

Logocal db shards

From this example, we can understand how data is distributed among different shards. Along with this, each shard will operate read and write operation that is associated with the information only. By this, the processing speed is increased and the processing upon data is decreased.

Sharding Key

While implementing sharding in MongoDB we have to define the key which will be treated as the primary key for the shared instance.

For Example, if we have a collection of student information of a particular class consisting of 14 students, along with which, we have two shard instances.

Sharding Key

Then the same collection is divided between these shards having 7/7 documents. To bind these two shard instances we have a common key which will reflect the relationship between these documents that will be known as the shard key. It may be numeric, compound or based on any hash.


In MongoDB, sharding is the approach of distributing information among different servers.

We can connect multiple servers with a single instance of a database to increase its scalability and availability of information. In short, the information is divided into chunks to store in different databases which are virtually working as a single database.

When a user is trying to fetch information from the database, it is collected from different servers in the form of chunks and then combined to deliver information to the user. This, in turn, will consume lesser time to search for information in database and process faster than conventional storage.

Stay tuned to our upcoming tutorial to know more about Replication in MongoDB.

PREV Tutorial | NEXT Tutorial