Q:

MongoDB tag range not working

I'm having some trouble with my MongoDB. Here's my configuration:
mongos> sh.status()
    --- Sharding Status --- 
      sharding version: {
            "_id" : 1,
            "version" : 4,
            "minCompatibleVersion" : 4,
            "currentVersion" : 5,
            "clusterId" : ObjectId("546f2d9c37d2895f3b8f9ec1")
    }
      shards:
            {  "_id" : "site_A_shard_1",  "host" : "site_A_shard_1/<HOST1>:27510,<HOST2>:27511",  "tags" : [ "site_A" ] }
            {  "_id" : "site_A_shard_2",  "host" : "site_A_shard_2/<HOST2>:27521,<HOST1>:27520",  "tags" : [ "site_A" ] }
            {  "_id" : "site_B_shard_1",  "host" : "site_B_shard_1/<HOST3>:27510,<HOST4>:27511",  "tags" : [ "site_B" ] }
            {  "_id" : "site_B_shard_2",  "host" : "site_B_shard_2/<HOST4>:27521,<HOST3>:27520",  "tags" : [ "site_B" ] }
      databases:
            {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
            {  "_id" : "test",  "partitioned" : false,  "primary" : "site_B_shard_1" }
            {  "_id" : "ocs",  "partitioned" : true,  "primary" : "site_B_shard_1" }
                    ocs.adrs_queue
                            shard key: { "shard.target" : 1, "shard.key" : 1 }
                            { "shard.target" : { "$minKey" : 1 }, "shard.key" : { "$minKey" : 1 } } -->> { "shard.target" : { "$maxKey" : 1 }, "shard.key" : { "$maxKey" : 1 } } on : site_B_shard_1 Timestamp(1, 0) 
                             tag: site_A  { "shard" : { "target" : "siteA", "key" : { "$minKey" : 1 } } } -->> { "shard" : { "target" : "siteA", "key" : { "$maxKey" : 1 } } }
                             tag: site_B  { "shard" : { "target" : "siteB", "key" : { "$minKey" : 1 } } } -->> { "shard" : { "target" : "siteB", "key" : { "$maxKey" : 1 } } }
                    ocs.ldrs
                            shard key: { "shard.target" : 1, "shard.key" : 1 }
                            { "shard.target" : { "$minKey" : 1 }, "shard.key" : { "$minKey" : 1 } } -->> { "shard.target" : { "$maxKey" : 1 }, "shard.key" : { "$maxKey" : 1 } } on : site_B_shard_1 Timestamp(1, 0) 
                             tag: site_A  { "shard" : { "target" : "siteA", "key" : { "$minKey" : 1 } } } -->> { "shard" : { "target" : "siteA", "key" : { "$maxKey" : 1 } } }
                             tag: site_B  { "shard" : { "target" : "siteB", "key" : { "$minKey" : 1 } } } -->> { "shard" : { "target" : "siteB", "key" : { "$maxKey" : 1 } } }
                    ocs.notifs_queue
                            shard key: { "shard.target" : 1, "shard.key" : 1 }
                            { "shard.target" : { "$minKey" : 1 }, "shard.key" : { "$minKey" : 1 } } -->> { "shard.target" : { "$maxKey" : 1 }, "shard.key" : { "$maxKey" : 1 } } on : site_B_shard_1 Timestamp(1, 0) 
                             tag: site_A  { "shard" : { "target" : "siteA", "key" : { "$minKey" : 1 } } } -->> { "shard" : { "target" : "siteA", "key" : { "$maxKey" : 1 } } }
                             tag: site_B  { "shard" : { "target" : "siteB", "key" : { "$minKey" : 1 } } } -->> { "shard" : { "target" : "siteB", "key" : { "$maxKey" : 1 } } }
                    ocs.ss_queue
                            shard key: { "shard.target" : 1, "shard.key" : 1 }
                            { "shard.target" : { "$minKey" : 1 }, "shard.key" : { "$minKey" : 1 } } -->> { "shard.target" : { "$maxKey" : 1 }, "shard.key" : { "$maxKey" : 1 } } on : site_B_shard_1 Timestamp(1, 0) 
                             tag: site_A  { "shard" : { "target" : "siteA", "key" : { "$minKey" : 1 } } } -->> { "shard" : { "target" : "siteA", "key" : { "$maxKey" : 1 } } }
                             tag: site_B  { "shard" : { "target" : "siteB", "key" : { "$minKey" : 1 } } } -->> { "shard" : { "target" : "siteB", "key" : { "$maxKey" : 1 } } }

Dispite of having a tag range configured it doesn't seem to work. Every time I try to insert some information it gets stored in the same shard (site_B_shard_1) even if the associated tag is site_A.
What am I doing wrong?

A:

It looks like you are trying to distribute a single collection across two shards, but have defined overlapping shard tag ranges where site_A and site_B include the whole range of shard.target key values (minkey .. maxkey):
tag: site_A  {
    "shard" : {
        "target" : "siteA",
        "key" : { "$minKey" : 1 }
    }
} -->> {
    "shard" : {
        "target" : "siteA",
        "key" : { "$maxKey" : 1 }
    }
}

tag: site_B  {
    "shard" : {
        "target" : "siteB",
        "key" : { "$minKey" : 1 }
    }
} -->> {
    "shard" : {
        "target" : "siteB",
        "key" : { "$maxKey" : 1 }
    }
}

If you want to distribute data within a collection using tag-aware sharding you need to define ranges where the lower bound is inclusive and the upper bound is exclusive, for example:

sh.addTagRange('ocs.adrs_queue',{"shard.target":MinKey},{"shard.target":"site_A"},'site_A')
  sh.addTagRange('ocs.adrs_queue',{"shard.target":"site_A"},{"shard.target":"site_B"},'site_A')
  sh.addTagRange('ocs.adrs_queue',{"shard.target":"site_B"},{"shard.target":MaxKey},'site_B')

In this set up:

documents with shard.target values matching site_A and site_B should be distributed to the matching shards
documents with shard.target values less than site_A will also be distributed on site_A shards
documents with shard.target values greater than site_B will be distributed on site_B shards

For some more examples of tag-aware sharding and multi-data centre deployment see:

How to Balance Collections Across Your Sharded Cluster
Tiered Storage Models in MongoDB: Optimizing Latency and Cost
MongoDB white paper on Multi-DC Deployments

