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?
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.