MongoDB supports tagging a range of shard key values to associate that range with a shard or group of shards. Those shards receive all inserts within the tagged range.
The balancer obeys tagged range associations, which enables the following deployment patterns:
This document describes the behavior, operation, and use of tag aware sharding in MongoDB deployments.
Hash-based sharding does not support tag-aware sharding.
The balancer migrates chunks of documents in a sharded collections to the shards associated with a tag that has a shard key range with an upper bound greater than the chunk’s lower bound.
If the chunks in sharded collection are already balanced, then the balancer will not migrate any chunks. If chunks in a sharded collection are not balanced, the balancer migrates chunks in tagged ranges to shards associated with those tags.
After configuring tags with a shard key range, and associating it with a shard or shards, the cluster may take some time to balance the data among the shards. This depends on the division of chunks and the current distribution of data in the cluster.
Once configured, the balancer respects tag ranges during future balancing rounds.
A single chunk may contain data with a shard key values that falls into ranges associated with more than one tag. To accommodate these situations, the balancer may migrate chunks to shards that contain shard key values that exceed the upper bound of the selected tag range.
Given a sharded collection with two configured tag ranges:
For this collection cluster, the balancer will migrate a chunk with shard key values ranging between 150 and 220 to a shard tagged NYC, since 150 is closer to 200 than 300.
To ensure that your collection has no potentially ambiguously tagged chunks, create splits on your tag boundaries. You can then manually migrate chunks to the appropriate shards, or wait for the balancer to automatically migrate these chunks.