On this page本页内容
In sharded clusters, you can create zones that represent a group of shards and associate one or more ranges of shard key values to that zone. 在分片集群中,可以创建表示一组分片的分区,并将一个或多个分片键值范围关联到该分区。MongoDB routes reads and writes that fall into a zone range only to those shards inside of the zone.MongoDB只将区域范围内的读写路由到区域内的分片。
Changed in version 4.0.3.在版本4.0.3中更改。
Associate a Zone with a particular shard using the 当连接到sh.addShardToZone()
method when connected to a mongos
instance. mongos
实例时,使用sh.addShardToZone()
方法将区域与特定分片关联。A single shard may have multiple zones, and multiple shards may also have the same zone.单个分片可能具有多个分区,多个分片也可能具有相同的分区。
The following example adds the zone 以下示例将区域NYC
to two shards, and the zones SFO
and NRT
to a third shard:NYC
添加到两个分片,将区域SFO
和NRT
添加到第三个分片:
sh.addShardToZone("shard0000", "NYC") sh.addShardToZone("shard0001", "NYC") sh.addShardToZone("shard0002", "SFO") sh.addShardToZone("shard0002", "NRT")
You may remove zone from a particular shard using the 当连接到sh.removeShardFromZone()
method when connected to a mongos
instance, as in the following example, which removes the NRT
zone from a shard:mongos
实例时,您可以使用sh.removeShardFromZone()
方法从特定分片中删除区域,如以下示例所示,该示例从分片中删除NRT区域:
sh.removeShardFromZone("shard0002", "NRT")
To define the zone's range of shard keys, use the 要定义区域的分片键范围,请在连接到sh.updateZoneKeyRange()
method when connected to a mongos
instance. mongos
实例时使用sh.updateZoneKeyRange()
方法。Any given shard key range may only have one assigned zone. 任何给定的分片键范围只能有一个指定区域。You cannot overlap defined ranges.不能重叠定义的范围。
Given a collection named 给定记录数据库中名为users
in the records
database, sharded by the zipcode
field. users
的集合,由zipcode
字段分片。The following operations assign:以下操作分配:
NYC
zoneNYC
区曼哈顿和布鲁克林的两个邮政编码范围SFO
zoneSFO
区的一个邮政编码范围sh.updateZoneKeyRange("records.users", { zipcode: "10001" }, { zipcode: "10281" }, "NYC") sh.updateZoneKeyRange("records.users", { zipcode: "11201" }, { zipcode: "11240" }, "NYC") sh.updateZoneKeyRange("records.users", { zipcode: "94102" }, { zipcode: "94135" }, "SFO")
Use the shell helper method 使用shell助手方法sh.removeRangeFromZone()
to remove a range from a zone.sh.removeRangeFromZone()
从区域中删除范围。
The following example removes the 以下示例删除了曼哈顿地区邮政编码范围的纽约市NYC
zone assignment for the range of zip codes within Manhattan:NYC
区域分配:
sh.removeRangeFromZone("records.user", {zipcode: "10001"}, {zipcode: "10281"})
Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.从MongoDB 4.0.2开始,删除集合将删除其关联的区域/标记范围。
Use 使用sh.status()
to list the zones associated to each shard in the cluster. sh.status()
列出与集群中每个分片关联的区域。You can also view a shards zones by querying the 您还可以通过查询shards
collection in the config
database.config
数据库中的分片集合来查看shards
区域。
The following example uses the 下面的示例使用find()
method to return all shards with the NYC
zone.find()
方法返回具有NYC区域的所有分片。
use config db.shards.find({ tags: "NYC" })
You can find zone ranges for all namespaces in the 您可以在配置数据库的tags
collection of the config
database. tags
集合中找到所有名称空间的区域范围。The output of sh.status()
also displays all zone ranges.sh.status()
的输出还显示所有区域范围。
The following example uses the 以下示例使用find()
method to return any range associated to the NYC
zone.find()
方法返回与NYC
区域关联的任何范围。
use config db.tags.find({ tag: "NYC" })