- Administration >
- Data Center Awareness >
- Tag Aware Sharding >
- Manage Shard Tags
Manage Shard Tags¶
On this page
In a sharded cluster, you can use tags to associate specific ranges of a shard key with a specific shard or subset of shards.
Tag a Shard¶
Associate tags with a particular shard using the
sh.addShardTag()
method when connected to a mongos
instance. A single shard may have multiple tags, and multiple shards
may also have the same tag.
Example
The following example adds the tag NYC
to two shards, and the tags
SFO
and NRT
to a third shard:
You may remove tags from a particular shard using the
sh.removeShardTag()
method when connected to a
mongos
instance, as in the following example, which removes
the NRT
tag from a shard:
Tag a Shard Key Range¶
To assign a tag to a range of shard keys use the
sh.addTagRange()
method when connected to a
mongos
instance. Any given shard key range may only have
one assigned tag. You cannot overlap defined ranges, or tag the same
range more than once.
Example
Given a collection named users
in the records
database,
sharded by the zipcode
field. The following operations assign:
- two ranges of zip codes in Manhattan and Brooklyn the
NYC
tag - one range of zip codes in San Francisco the
SFO
tag
Note
Shard ranges are always inclusive of the lower value and exclusive of the upper boundary.
Remove a Tag From a Shard Key Range¶
The mongod
does not provide a helper for removing a tag
range. You may delete tag assignment from a shard key range by removing
the corresponding document from the tags
collection of
the config
database.
Each document in the tags
holds the namespace
of the sharded collection and a minimum shard key value.
Example
The following example removes the NYC
tag assignment for the
range of zip codes within Manhattan:
View Existing Shard Tags¶
The output from sh.status()
lists tags associated with a
shard, if any, for each shard. A shard’s tags exist in the shard’s
document in the shards
collection of the config
database. To return all shards with a specific tag, use a sequence of
operations that resemble the following, which will return only those
shards tagged with NYC
:
You can find tag ranges for all namespaces in the
tags
collection of the config
database. The output
of sh.status()
displays all tag ranges. To return all shard
key ranges tagged with NYC
, use the following sequence of
operations: