Database Manual / Reference / Database Commands / Sharding

updateZoneKeyRange (database command数据库命令)

Definition定义

updateZoneKeyRange

The updateZoneKeyRange administrative command can either create or remove the association between a range of shard key values and a zone.updateZoneKeyRange管理命令可以创建或删除分片键值范围与区域之间的关联。

You can run updateZoneKeyRange database command and its helpers sh.updateZoneKeyRange() and sh.addTagRange() on an unsharded collection or a non-existing collection.您可以在未记录的集合或不存在的集合上运行updateZoneKeyRange数据库命令及其助手sh.updateZoneKeyRange()sh.addTagRange()

Tip

In mongosh, this command can also be run through the sh.updateZoneKeyRange() helper method.mongosh中,此命令也可以通过sh.updateZoneKeyRange()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. 助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。 In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要便利性或需要额外的返回字段,请使用数据库命令。

To run updateZoneKeyRange, use the db.runCommand( { <command> } ) method.要运行updateZoneKeyRange,请使用db.runCommand( { <command> } )方法。

You must run addShardToZone on the admin database.您必须在管理数据库上运行addShardToZone

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
updateZoneKeyRange: <string>,
min: <document>,
max: <document>,
zone: <string> | <null>
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Parameter参数Type类型Description描述
updateZoneKeyRangestring字符串

The namespace of the collection to associate with the range.与范围关联的集合的命名空间。

The collection must be sharded for the command to succeed.必须对集合进行分片,命令才能成功执行。

mindocument文档

The inclusive lower bound of the range of shard key values.分片键值范围的包含下限。

Specify each field of the shard key in the form of <fieldname> : <value>. The value must be of the same BSON type or types as the shard key.<fieldname> : <value>的形式指定分片键的每个字段。该值必须与分片键具有相同的BSON类型。

To use hashed sharding, the field value of min needs to be of type NumberLong.要使用哈希分片,min的字段值需要为NumberLong类型。

maxdocument文档

The exclusive upper bound of the range of shard key values.分片键值范围的唯一上限。

Specify each field of the shard key in the form of <fieldname> : <value>. The value must be of the same BSON type or types as the shard key.<fieldname> : <value>的形式指定分片键的每个字段。该值必须与分片键具有相同的BSON类型。

To use hashed sharding, the field value of max needs to be of type NumberLong.要使用哈希分片,max的字段值需要为NumberLong类型。

zonestring字符串

The name of the zone to associate with the range bounded by the min and max.minmax所限定的范围关联的区域的名称。

If the value does not match an existing zone, the command fails.如果该值与现有区域不匹配,则命令失败。

Specify null to remove the association between the range with lower bounds of min and upper bound of max and the updateZoneKeyRange collection. 指定null可删除下限为min、上限为max的范围与updateZoneKeyRange集合之间的关联。The values of min and max must match exactly the target range.minmax的值必须与目标范围完全匹配。

Behavior行为

If no zone range matches the minimum and maximum bounds passed to updateZoneKeyRange, nothing is removed.如果没有区域范围与传递给updateZoneKeyRange的最小和最大边界匹配,则不会删除任何内容。

Only issue updateZoneKeyRange when connected to a mongos instance.仅在连接到mongos实例时发出updateZoneKeyRange

mongosh provides two helper methods:提供了两种辅助方法:

  • sh.updateZoneKeyRange() for adding a range of shard key values to a zone.用于向区域添加一系列分片键值。
  • sh.removeRangeFromZone() for removing a range of shard key values from a zone.用于从区域中删除一系列分片键值。

You cannot create a range of shard key values whose lower and upper boundaries overlap with an existing range for the sharded collection. 您不能创建其下限和上限与分片集合的现有范围重叠的分片键值范围。For example, given an existing range of 1 to 10, you cannot create a new range of 5 to 20, as the new range would overlap with the existing range.例如,给定110的现有范围,您无法创建520的新范围,因为新范围将与现有范围重叠。

A zone can have multiple ranges of data associated with it, but a range can at most be associated with a single zone.一个区域可以有多个与之关联的数据范围,但一个范围最多只能与一个区域关联。

When removing the association between a range and a zone, updateZoneKeyRange does not remove the zone. 删除区域和区域之间的关联时,updateZoneKeyRange不会删除区域。Use the removeShardFromZone command to remove the association between a zone and a shard.使用removeShardFromZone命令删除区域和分片之间的关联。

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

Initial Chunk Distribution for Empty or Non-Existing Collections空集合或不存在集合的初始块分布

If you are considering performing zone sharding on an empty or non-existent collection, use updateZoneKeyRange to create the zones and zone ranges before sharding the collection (since 4.0.2). 如果您正在考虑对空的或不存在的集合执行区域分片,请在分片集合之前使用updateZoneKeyRange创建区域和区域范围(从4.0.2开始)。Starting in version 4.0.3, creating zones and zone ranges on empty or non-existing collections allows MongoDB to optimize the initial chunk creation and distribution process when sharding the collection. 从4.0.3版本开始,在空的或不存在的集合上创建区域和区域范围允许MongoDB在对集合进行分片时优化初始块创建和分发过程。This optimized process supports faster setup of zoned sharding with less balancer overhead than creating zones after sharding. The balancer performs all chunk management after the optimized initial chunk creation and distribution.这种优化的过程支持更快地设置分区分片,与分片后创建区域相比,平衡器开销更少。平衡器在优化的初始块创建和分发之后执行所有块管理。

For an example of defining zones and zone ranges for initial chunk distribution, see Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection.有关为初始块分布定义分区和分区范围的示例,请参阅为空或不存在的集合预先定义分区和区域范围

Initial Chunk Distribution with Compound Hashed Shard Keys具有复合哈希分片键的初始块分布

MongoDB supports sharding collections on compound hashed indexes. MongoDB can perform optimized initial chunk creation and distribution when sharding the empty or non-existing collection on a compound hashed shard key.MongoDB支持在复合哈希索引上对集合进行分片。MongoDB可以在对复合哈希分片键上的空或不存在的集合进行分片时,执行优化的初始块创建和分发。

Hashed Field is prefix哈希字段是前缀

If the hashed field is the prefix of the shard key (i.e. the first field in the shard key), all of the following must be true for MongoDB to perform initial chunk creation and distribution:如果哈希字段是分片键的前缀(即分片键中的第一个字段),则MongoDB必须满足以下所有条件才能执行初始块创建和分发:

Hashed Field is Not Prefix哈希字段不是前缀

If the hashed field is not the prefix of the shard key (i.e. the shard key has one or more non-hashed leading fields), all of the following must be true for MongoDB to perform initial chunk creation and distribution:如果哈希字段不是分片键的前缀(即分片键有一个或多个非哈希前导字段),则MongoDB必须满足以下所有条件才能执行初始块创建和分发:

  • The collection has one zone range for each combination of distinct prefix field values (i.e. all fields preceding the hashed field).对于不同前缀字段值的每种组合(即哈希字段之前的所有字段),该集合都有一个区域范围。
  • For the lower-bound of each zone range, specify MinKey for the hashed field and all subsequent fields.对于每个区域范围的下限,为哈希字段和所有后续字段指定MinKey
  • For each zone range, at least one upper-bound prefix field must differ from its lower-bound counterpart.对于每个区域范围,至少有一个上限前缀字段必须与其下限对应字段不同。
  • sh.shardCollection() specifies the presplitHashedZones: true option.指定presplitHashedZones:true选项。

For a more complete example of defining zones and zone ranges for initial chunk distribution on a compound hashed shard key, see Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection.有关在复合哈希分片键上定义初始块分布的区域和区域范围的更完整示例,请参阅空或不存在集合的预定义区域和区域区域范围

Balancer平衡器

After successfully running updateZoneKeyRange, there may be chunk migrations during the next balancer round.成功运行updateZoneKeyRange后,下一轮平衡器可能会进行块迁移。

After adding a range to a zone, the balancer must first run in order to migrate any chunks whose ranges are covered by the zone to shards inside of that zone. Until balancing completes, some chunks may reside on the wrong shard given the configured zones for the sharded cluster.在向区域添加范围后,平衡器必须首先运行,以便将区域覆盖范围的任何块迁移到该区域内的分片。在平衡完成之前,根据分片集群的配置区域,一些块可能会驻留在错误的分片上。

Removing the association between a range and a zone removes the constraints keeping chunks covered by the range on the shards inside that zone. During the next balancer round, the balancer may migrate chunks that were previously covered by the zone.删除范围和区域之间的关联会删除该区域内分片上保持范围覆盖块的约束。在下一轮平衡器中,平衡器可能会迁移之前被该区域覆盖的块。

See the documentation for the sharded cluster balancer for more information on how migrations work in a sharded cluster.有关迁移如何在分片集群中工作的更多信息,请参阅分片集群平衡器的文档。

Bounds边界

Zone ranges are always inclusive of the lower boundary and exclusive of the upper boundary.区域范围始终包括下限,不包括上限。

Dropped Collections已删除集合

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

In earlier versions, MongoDB does not remove the tag associations for a dropped collection, and if you later create a new collection with the same name, the old tag associations will apply to the new collection.在早期版本中,MongoDB不会删除已删除集合的标签关联,如果稍后创建同名的新集合,旧的标签关联将应用于新集合。

Security安全

For sharded clusters running with authentication, you must authenticate as either:对于使用身份验证运行的分片集群,您必须以以下方式之一进行身份验证

  • a user whose privileges include the specified actions on various collections in the config database:权限包括对config数据库中的各种集合执行指定操作的用户:

    or, alternatively,或者,

  • a user whose privileges include enableSharding on the cluster resource.其权限包括在集群资源上enableSharding(启用分片)的用户。

The clusterAdmin or clusterManager built-in roles have the appropriate permissions for issuing updateZoneKeyRange. See the documentation page for Role-Based Access Control for more information.clusterAdminclusterManager内置角色具有发布updateZoneKeyRange的适当权限。有关更多信息,请参阅基于角色的访问控制的文档页面。

Example示例

Given a sharded collection exampledb.collection with a shard key of { a : 1 }, the following operation creates a range with a lower bound of 1 and an upper bound of 10 on the alpha zone:给定一个分片集合exampledb.collection,其分片键为{ a : 1 },以下操作在alpha区域创建一个下限为1、上限为10的范围:

admin = db.getSiblingDB("admin")
admin.runCommand(
{
updateZoneKeyRange : "exampledb.collection",
min : { a : 1 },
max : { a : 10 },
zone : "alpha"
}
)

The following operation removes the previously created range by passing null to the zone field.以下操作通过向zone字段传递null来删除之前创建的范围。

admin = db.getSiblingDB("admin")
admin.runCommand(
{
updateZoneKeyRange : "exampledb.collection",
min : { a : 1 },
max : { a : 10 },
zone : null
}
)

The min and max must match exactly the bounds of the target range. The following operation attempts to remove the previously created range, but specifies { a : 0 } as the min bound:minmax必须与目标范围的边界完全匹配。以下操作尝试删除以前创建的范围,但将{ a : 0 }指定为min界限:

admin = db.getSiblingDB("admin")
admin.runCommand(
{
updateZoneKeyRange : "exampledb.collection",
min : { a : 0 },
max : { a : 10 },
zone : null
}
)

While the range of { a : 0 } and { a : 10 } encompasses the existing range, it is not an exact match and therefore updateZoneKeyRange does not remove anything.虽然{ a : 0 }{ a : 10 }的范围包含现有范围,但它不是完全匹配的,因此updateZoneKeyRange不会删除任何内容。

Compound Shard Key复合分片键

Given a sharded collection exampledb.collection with a shard key of { a : 1, b : 1 }, the following operation creates a range covering the lower bound of { a: 1, b : 1 } and an upper bound of { a : 10, b : 10} and associates it with the alpha zone:给定一个分片集合exampledb.collection,其分片键为{ a : 1, b : 1 },以下操作将创建一个覆盖{ a: 1, b : 1 }下限和{ a : 10, b : 10}上限的范围,并将其与alpha区域相关联:

admin = db.getSiblingDB("admin")
admin.runCommand(
{
updateZoneKeyRange : "exampledb.collection",
min : { a : 1, b : 1 },
max : { a : 10, b : 10 },
zone : "alpha"

}
)