Docs HomeMongoDB Manual

sh.addShardToZone()

Definition定义

sh.addShardToZone(shard, zone)

Associates a shard with a zone. 将分片与区域关联。MongoDB associates this shard with the given zone. Chunks that are covered by the zone are assigned to shards associated with the zone.MongoDB将这个分片与给定的区域相关联。区域覆盖的区块将分配给与该区域关联的分片。

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the addShardToZone command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

This method has the following parameter:此方法具有以下参数:

Parameter参数Type类型Description描述
shardstringThe name of the shard to which to associate the zone. 要将区域关联到的分片的名称。
zonestringThe name of the zone to associate with the shard. 要与分片关联的区域的名称。

Only issue sh.addShardToZone() when connected to a mongos instance.仅在连接到mongos实例时发出sh.addShardToZone()

Behavior行为

You can associate a zone with multiple shards, and a shard can associate with multiple zones.您可以将一个区域与多个分片相关联,并且一个分片可以与多个区域相关联。

See the zone manual page for more information on zones in sharded clusters.有关分片集群中区域的更多信息,请参阅区域手册页面。

Ranges范围

MongoDB effectively ignores zones that do not have at least one range of shard key values associated with it.MongoDB有效地忽略了那些至少没有一个与之相关联的分片键值范围的区域。

To associate a range of shard key values with a zone, use the sh.updateZoneKeyRange() method.要将一系列分片键值与区域关联,请使用sh.updateZoneKeyRange()方法。

Starting in MongoDB 4.0.2, you can run updateZoneKeyRange database command and its helpers sh.updateZoneKeyRange() and sh.addTagRange() on an unsharded collection or a non-existing collection.从MongoDB 4.0.2开始,您可以在未排序的集合或不存在的集合上运行updateZoneKeyRange数据库命令及其助手sh.updateZoneKeyRange()sh.addTagRange()

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.有关示例,请参阅空集合或不存在集合的预定义分区和分区范围

Security安全

For sharded clusters that enforce access control, you must authenticate as a user whose privileges include either:对于强制访问控制的分片集群,您必须以用户身份进行身份验证,该用户的权限包括:

  • update on the shards collection in the config database; or, alternatively,config数据库中的shards集合上的update;或者可替换地,
  • enableSharding on the cluster resource (Starting in version 4.2.2).群集资源上的enableSharding(从版本4.2.2开始)。

The clusterAdmin or clusterManager built-in roles have the appropriate permissions for issuing sh.addShardToZone(). See the Role-Based Access Control manual page for more information.clusterAdminclusterManager内置角色具有发出sh.addShardToZone()的适当权限。有关详细信息,请参阅基于角色的访问控制手册页面。

Example实例

The following example adds three zones, NYC, LAX, and NRT, associating each to a shard:以下示例添加了三个区域,NYCLAXNRT,将每个区域关联到一个分片:

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:一个分片可以与多个区域关联。以下示例将LGAshard0000关联起来:

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

shard0000 associates with both the LGA zone and the JFK zone. shard0000同时与LGA区域和JFK区域相关联。In a balanced cluster, MongoDB routes reads and writes covered by either zone to shard0000.在一个平衡的集群中,MongoDB将任一区域覆盖的读写路由到shard0000

Tip