How shard a collection with data inside sharded cluster MongoDB

0

I want to shard a collection with data. When I try with sh.shardCollection(“myDb.myCollection”, {id:“hashed”}) then this collection shard but it’s not spread to the whole shards. only spread to the primary shard. for example,

In this picture when I shard the empty collection it’s split into 4 chunks. But the previously created collection, when going to shard it, is split into one chunk(primary shard)

My question is how correctly shard a collection with data in MongoDB. Have any other alternative way?

Hi @Lakshan_Amal ,

Welcome to The MongoDB Community Forums! :wave:

As per this documentation on Sharding a Populated Collection

If you shard a populated collection using a hashed shard key:

  • The sharding operation creates the initial chunk(s) to cover the entire range of the shard key values. The number of chunks created depends on the configured chunk size.
  • After the initial chunk creation, the balancer migrates these initial chunks across the shards as appropriate as well as manages the chunk distribution going forward.

Your collection only has 1 chunk, and therefore can’t be divided among the shards. The Default maximum size for a chunk is 64MB. Depending on the version of MongoDB either the mongos or the mongod will call for a split when it realizes that a significant fraction of the maximum chunk size has been written to a chunk or you can manually split the chunk using sh.splitAt.

You can also run below query to get information about the number and size of documents/chunks on each shard.

db.getSiblingDB("myDb").myCollection.getShardDistribution()

Also if you are using any version of MongoDB below MongoDB 6.0 then you must enable sharding on database and on collection level:

sh.enableSharding("myDb")
sh.shardCollection("myDb.myCollection", { _id : 1 })

Regards,
Tarun

Thank you for your answer. I got your point. Very thankful to you.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.