MongoDB Enable Sharding

Enable Sharding

In order to shard a collection in mongodb, you need to set up a structure in accordance with the following criterias. Note: There is no rollback sharding process before mongodb version 5.0.

The sharding key should be based on an index consisting of the commonly used query filter keys. In order to guarantee balanced distribution the sharding key should adhere the following guidelines:

Balanced frequency: The values of the field(s) in the documents should not have high repetition frequency. If one value or group of values is occurring at high frequency, the data with those values will not be distributed in a balanced manner.
High cardinality: The range of values the shard key fields have in the data should be high. If the field values have a low range of values, the distribution will be limited to those ranges alone.
Non Monotonic: The fields chosen for the shard key should not be a type with monotonically increasing or decreasing values (such as timestamp). While the distribution may be balanced at first, over time the new documents will create an accumulation undistributed data on the first or last shard.

It is recommend to run the sharding procedure for one collection at a time and waiting for the balancing to complete before sharding the next collection.

If the data in all the target collection has identical structure, the same shard key index can be used. However if the data structures are different, each collection should be analysed separately based on applied filters and documents structure.

Sharding the data is a critical process that is not easily undone as such we strongly recommend considering our professional services assistance for this procedure. I will reach out to the accounts team to review available options with you.

EXAMPLE;

use test

sh.enableSharding(“test”) if u enable no way to rollback this operation before mongodb version 5.0

db.collectionName.createIndex( { “id” : 1 } )  

sh.shardCollection(“test.collectionName”, {“id” : 1 } ) 

Related Post

One thought on “MongoDB Enable Sharding

Leave a Reply

Your email address will not be published. Required fields are marked *