Mongodb sharding – set shard size

mongodbmongodb-3.4sharding

I am very new to MongoDB and am trying to do database sharding.

I have created two MongoDB shards and inserted 10000 records in it. I have used shard key as "no" which increments by 1 in every document in ascending order.

mongos>sh.addShard("localhost:27004",{"no":1})
mongos>sh.addShard("localhost:27005",{"no":1})

Here, when I insert the records, the shard 1 gets only 6 records and shard 2 gets the rest of the records.

The output of sh.shatus() is:

 databases:
    {  "_id" : "shard_test_2",  "primary" : "shard0000",  "partitioned" : true }
            shard_test_2.test
                    shard key: { "no" : 1 }
                    unique: false
                    balancing: true
                    chunks:
                            shard0000       2
                            shard0001       1
                    { "no" : { "$minKey" : 1 } } -->> { "no" : 1 } on : shard0000 Timestamp(2, 1)
                    { "no" : 1 } -->> { "no" : 6 } on : shard0000 Timestamp(1, 2)
                    { "no" : 6 } -->> { "no" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 0)

Now, I want my shard one to store more documents than 6.

I read that how many documents will go to which shard is the function of balancer. So, how do I configure the balancer?

Or is there any other way of setting how much data should go to which shard?
No resource online is helping much in this.

Best Answer

Mongo can't determine key ranges for sharding if you use increments. Here's an example:

You have a collection with keys 10, 20, 30, 40, 50, 60.

If mongo have to create two shards, it may assume the keys range as: [10,30] and [31,60] (or similar).

By continuing to write bigger keys, they will always go to the second range.

Mongo will adjust the ranges, but it can never know what the next key will be, and it will always put it into the last range.

Try to use some key with good distribution.

More details here: https://docs.mongodb.com/manual/core/sharding-shard-key/#monotonically-changing-shard-keys