Mongodb – How to we restrict collections on specific shards of Mongodb while using Tag Awareness Sharding

mongodbsharding

We have a Mongodb cluster which has 10 shards (0-9) and two different applications supposedly as APP1 and APP2. Both APP1 and APP2 have different data model and shard keys.

We want that to assign shards 0 to 5 to APP1 and 6-9 to APP2. For that we have created tags for each shards and have assigned these tags with a range to each application using the following commands:

sh.addShardTag('shard0', 'APP1_V0')
sh.addTagRange("test.collectiontest", {"_id.s":0,"_id.i" : MinKey}, {"_id.s":0, "_id.i" : MaxKey}, "APP1_V0")

We have added tags ranges similarily on each of shards based the shard key of the applications.

The problem that we are facing while creating collection, the primary shard i.e assigned to the collection is different from tags assigned to the collection. For eg, App1 collection is tagged to shards 0-5 but when i'm a creating the collection , primary shard assigned to the collection is shard7.

I checked the primary using the sh.status() command.

Can we control this behaviour i.e while creating the collection assign it to the specific shard.

Mongodb Version : 3.4.2

Best Answer

The answer is no. Tags only "guide" where balancer moves chunks. You need to use movePrimary command after creating DB to "change" that primary shard.