unable to add a range to a tag using sh.addTagRange command Have setup a 4 node test shard cluster:


1- mongos
1- config server
2- shard nodes hosting 1 shard each for a collection.


Cluster is working perfectly, but when trying to run the following command:

mongos&gt; sh.addTagRange("HTMLDumps.HTMLRepository", { ShardMapId: "7" }, { ShardMapId: "11" }, "NRT")

 "errmsg" : "min: { ShardMapId: \"7\" } should be less than max: { ShardMapId: \"11\" }",
        "code" : 9,
        "codeName" : "FailedToParse",


Below is the shard status output:

databases:
        {  "_id" : "HTMLDumps",  "primary" : "shard0001",  "partitioned" : true }
                HTMLDumps.HTMLRepository
                        shard key: { "ShardMapId" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard0000       2
                                shard0001       2
                        { "ShardMapId" : { "$minKey" : 1 } } --&gt;&gt; { "ShardMapId" : "" } on : shard0001 Timestamp(3, 1)
                        { "ShardMapId" : "" } --&gt;&gt; { "ShardMapId" : "1" } on : shard0001 Timestamp(1, 2) jumbo
                        { "ShardMapId" : "1" } --&gt;&gt; { "ShardMapId" : "7" } on : shard0000 Timestamp(2, 0)
                        { "ShardMapId" : "7" } --&gt;&gt; { "ShardMapId" : { "$maxKey" : 1 } } on : shard0000 Timestamp(3, 0)


                             tag: NYC  { "ShardMapId" : "1" } -->> { "ShardMapId" : "7" }

What am I missing?

Complete sh.status output:
Shard key zone (tag) ranges require specifying minimum and maximum bounds for a range of shard key values.

Since your shard key values are currently strings, they will be compared using binary order rather than numerical order. The error message is correctly indicating that a minimum value of "7" (string) is not less than a value of "11" (string). For example, given the strings "1", "7", and "11" the expected ascending sort outcome using a binary string comparison is "1", "11", and "7". This is roughly alphabetical order, but more specifically strings in MongoDB are stored as UTF-8 bytes.

As you are intending on using zone sharding, for this test deployment it would be better to have numeric shard key values so you can use the range of 1-7 for your zone. You could remove and replace existing documents or just add more with the desired shard key values.

I assume you aren't concerned about the cardinality of your shard key for a test cluster, but for a real deployment you may want to choose a more appropriate shard key if ShardMapId will only have a small range of values.

Note: tag-aware sharding was succeeded by Zone sharding in MongoDB 3.4. The sh.addTagRange() shell helper has been aliased to the modern equivalent of sh.updateZoneKeyRange(), but I would recommend using the newer helpers to be safe. You should also ensure that your shell version() matches your server db.version().