Docs HomeMongoDB Manual

Manage Shard Zones管理分片区域

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只将属于区域范围的读写路由到区域内的分片。

Tip

By defining the zones and the zone ranges before sharding an empty or a non-existing collection, the shard collection operation creates chunks for the defined zone ranges as well as any additional chunks to cover the entire range of the shard key values and performs an initial chunk distribution based on the zone ranges. 通过在对空集合或不存在的集合进行分片之前定义区域和区域范围,分片集合操作为定义的区域范围以及任何额外的块创建块,以覆盖分片键值的整个范围,并基于区域范围执行初始块分布。This initial creation and distribution of chunks allows for faster setup of zoned sharding. After the initial distribution, the balancer manages the chunk distribution going forward.这种块的初始创建和分布允许更快地设置分区分片。在初始分发之后,平衡器管理接下来的块分发。

See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for an example.有关示例,请参阅空集合或不存在集合的预定义分区和分区范围

Add Shards to a Zone将分片添加到区域

Associate a Zone with a particular shard using the sh.addShardToZone() method when connected to a mongos instance. 当连接到mongos实例时,使用sh.addShardToZone()方法将Zone与特定的分片相关联。A single shard may have multiple zones, and multiple shards may also have the same zone.单个分片可能具有多个区域,多个分片也可能具有相同的区域。

Example

The following example adds the zone NYC to two shards, and the zones SFO and NRT to a third shard:以下示例将区域NYC添加到两个分片,将区域SFONRT添加到第三个分片:

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")

Create a Zone Range创建分区范围

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.任何给定的分片键范围都只能有一个指定的区域。不能重叠已定义的范围。

Example

Given a collection named users in the records database, sharded by the zipcode field. records数据库中给定一个名为users的集合,由zipcode字段进行分片。The following operations assign:以下操作分配:

  • two ranges of zip codes in Manhattan and Brooklyn the NYC zoneNYC纽约曼哈顿和布鲁克林区的两个邮政编码范围
  • one range of zip codes in San Francisco the SFO zone旧金山的一个邮政编码范围SFO区域
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")
Note
  • Zone ranges are always inclusive of the lower boundary and exclusive of the upper boundary.区域范围始终包括下边界,不包括上边界。
  • Dropping a collection deletes its associated zone/tag ranges.删除集合将删除其关联的区域/标记范围。

Remove a Zone Range删除区域范围

Use the shell helper method sh.removeRangeFromZone() to remove a range from a zone.使用shell辅助方法sh.removeRangeFromZone()从区域中移除范围。

Example

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"})
Note

Dropping a collection deletes its associated zone/tag ranges.删除集合将删除其关联的区域/标记范围。

View Existing Zones查看现有分区

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. 您可以在config数据库的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" })