Docs 菜单

sh.addShardToZone()

sh.addShardToZone(shard, zone)

Associates a shard with a 区域. MongoDB associates this shard with the given zone. Chunks that are covered by the zone are assigned to shards associated with the zone.

重要

mongosh 方法

本页面提供 mongosh 方法的相关信息。这不是数据库命令或特定语言驱动程序(例如 Node.js)的相关文档。

有关数据库命令,请参阅 addShardToZone 命令。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

This method has the following parameter:

Parameter
类型
说明

字符串

The name of the shard to which to associate the 区域.

字符串

The name of the zone to associate with the 分片.

仅在连接到 实例时发出sh.addShardToZone() mongos

此方法可用于以下环境中托管的部署:

重要

M0 、M2 、M5 和 Flex 集群不支持此命令。有关更多信息,请参阅不支持的命令。

You can associate a zone with multiple shards, and a shard can associate with multiple zones.

有关分片集群中区域的更多信息,请参阅区域手册页面。

MongoDB effectively ignores zones that do not have at least one range of shard key values associated with it.

To associate a range of shard key values with a zone, use the sh.updateZoneKeyRange() method.

You can run updateZoneKeyRange database command and its helpers sh.updateZoneKeyRange() and sh.addTagRange() on an unsharded collection or a non-existing collection.

提示

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.

有关示例,请参阅为空集合或不存在的集合预先定义区域和区域范围

For sharded clusters that enforce access control, you must authenticate as a user whose privileges include either:

The clusterAdmin or clusterManager built-in roles have the appropriate permissions for issuing sh.addShardToZone(). See the 基于角色的访问控制 manual page for more information.

The following example adds three zones, NYC, LAX, and NRT, associating each to a shard:

sh.addShardToZone("shard0000", "JFK")
sh.addShardToZone("shard0001", "LAX")
sh.addShardToZone("shard0002", "NRT")

A shard can associate with multiple zones. The following example associates LGA to shard0000:

sh.addShardToZone("shard0000", "LGA")

shard0000 associates with both the LGA zone and the JFK zone. In a balanced cluster, MongoDB routes reads and writes covered by either zone to shard0000.