On this page本页内容
updateZoneKeyRange
The updateZoneKeyRange
administrative command can either create or remove the association between a range of shard key values and a zone.updateZoneKeyRange
管理命令可以创建或删除分片键值范围与区域之间的关联。
Starting in MongoDB 4.0.2, you can run 从MongoDB 4.0.2开始,您可以在未分级集合或不存在的集合上运行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()
。
To run 要运行updateZoneKeyRange
, use the db.runCommand( { <command> } )
method.updateZoneKeyRange
,请使用 db.runCommand( { <command> } )
方法。
You must run 您必须在管理数据库上运行addShardToZone
on the admin database.addShardToZone
。
The updateZoneKeyRange
command has the following syntax:updateZoneKeyRange
命令具有以下语法:
{ updateZoneKeyRange: <string>, min: <document>, max: <document>, zone: <string> | <null> }
The command takes the following fields:该命令接受以下字段:
updateZoneKeyRange | string |
|
min | document |
|
max | document |
|
zone | string |
|
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()
sh.removeRangeFromZone()
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.有关分片化集群中的区域的更多信息,请参阅区域手册页面。
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.有关为初始区块分布定义区域和区域范围的示例,请参阅为空集合或非现有集合预先定义区域和分区范围。
Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. 从4.4版开始,MongoDB支持在复合哈希索引上对集合进行分片。MongoDB can perform optimized initial chunk creation and distribution when sharding the empty or non-existing collection on a compound hashed shard key.MongoDB可以在将空的或不存在的集合分片到复合哈希分片键上时,执行优化的初始块创建和分发。
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 集合具有单个区域范围,所有下限字段为MinKey
for all lower-bound fields and MaxKey
for all upper-bound fields.MinKey
,所有上限字段为MaxKey
。
sh.shardCollection()
specifies the presplitHashedZones: true option.指定presplitHashedZones: true
选项。
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.有关为复合哈希分片键上的初始块分布定义区域和区域范围的更完整示例,请参阅为空集合或非现有集合预先定义区域和分区范围。
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.有关如何在分片集群中进行迁移的更多信息,请参阅分片集群平衡器的文档。
Zone ranges are always inclusive of the lower boundary and exclusive of the upper boundary.区域范围始终包括下边界,不包括上边界。
Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.从MongoDB 4.0.2开始,删除集合将删除其关联的区域/标记范围。
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不会删除已删除集合的标记关联,如果稍后创建同名的新集合,则旧标记关联将应用于新集合。
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
数据库中的各种集合执行指定操作的用户:
find
on the config.shards
collectionconfig.shards
集合中find
find
, update
, and remove
on the config.tags
collection;config.tags
集合上find
、update
和remove
;or, alternatively,或者,
enableSharding
on the cluster resource (available starting in version 4.2.2, 4.0.14, 3.6.16).enableSharding
的用户(从版本4.2.2、4.0.14、3.6.16开始提供)。The clusterAdmin
or clusterManager
built-in roles have the appropriate permissions for issuing updateZoneKeyRange
. clusterAdmin
或clusterManager
内置角色具有发出updateZoneKeyRange
的适当权限。See the documentation page for Role-Based Access Control for more information.有关详细信息,请参阅基于角色的访问控制的文档页。
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:{ a : 1 }
的分片集合exampledb.collection
,以下操作在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.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. min
和max
必须与目标范围的边界完全匹配。The following operation attempts to remove the previously created range, but specifies 以下操作尝试删除以前创建的范围,但将{ a : 0 }
as the min
bound:{ 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
不会删除任何内容。
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" } )