Definition定义
updateZoneKeyRangeTheupdateZoneKeyRangeadministrative command can either create or remove the association between a range of shard key values and a zone.updateZoneKeyRange管理命令可以创建或删除分片键值范围与区域之间的关联。You can run您可以在未记录的集合或不存在的集合上运行updateZoneKeyRangedatabase command and its helperssh.updateZoneKeyRange()andsh.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 thesh.updateZoneKeyRange()helper method.mongosh中,此命令也可以通过sh.updateZoneKeyRange()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, 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 thedb.runCommand( { <command> } )method.updateZoneKeyRange,请使用db.runCommand( { <command> } )方法。You must run您必须在管理数据库上运行addShardToZoneon 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:该命令包含以下字段:
updateZoneKeyRange |
| |
min |
| |
max |
| |
zone |
|
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.1到10的现有范围,您无法创建5到20的新范围,因为新范围将与现有范围重叠。
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必须满足以下所有条件才能执行初始块创建和分发:
The collection has a single zone range with该集合只有一个区域范围,所有下限字段都有MinKeyfor all lower-bound fields andMaxKeyfor all upper-bound fields.MinKey,所有上限字段都有MaxKey。sh.shardCollection()specifies the presplitHashedZones: true option.指定presplitHashedZones: true选项。
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对于每个区域范围的下限,为哈希字段和所有后续字段指定MinKeyfor 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权限包括对configdatabase:config数据库中的各种集合执行指定操作的用户:在findon theconfig.shardscollectionconfig.shards集合中find在find,update, andremoveon theconfig.tagscollection;config.tags集合上find、update和remove;
or, alternatively,或者,a user whose privileges include其权限包括在集群资源上enableShardingon 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.clusterAdmin或clusterManager内置角色具有发布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:min和max必须与目标范围的边界完全匹配。以下操作尝试删除以前创建的范围,但将{ 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"
}
)