定义
updateZoneKeyRangeThe
updateZoneKeyRangeadministrative command can either create or remove the association between a range of shard key values and a zone.You can run
updateZoneKeyRangedatabase command and its helperssh.updateZoneKeyRange()andsh.addTagRange()on an unsharded collection or a non-existing collection.提示
In
mongosh, this command can also be run through thesh.updateZoneKeyRange()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.要运行
updateZoneKeyRange,请使用db.runCommand( { <command> } )方法。您必须在管理员数据库上运行
addShardToZone。
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.adminCommand( { updateZoneKeyRange: <string>, min: <document>, max: <document>, zone: <string> | <null> } )
命令字段
该命令接受以下字段:
Parameter | 类型 | 说明 |
|---|---|---|
| 字符串 | 与范围关联的collection的命名空间。 必须对collection进行分片才能使命令成功。 |
| 文档 | 分片键值范围的下限(含)。 以 要使用哈希分片, |
| 文档 | 分片键值范围的独占上限。 以 要使用哈希分片, |
| 字符串 | 与 如果该值与现有区域不匹配,则命令失败。
|
行为
If no zone range matches the minimum and maximum bounds passed to updateZoneKeyRange, nothing is removed.
Only issue updateZoneKeyRange when connected to a mongos instance.
mongosh 提供了两种辅助方法:
sh.updateZoneKeyRange()用于向区域添加一系列分片键值。sh.removeRangeFromZone()用于从区域中删除一系列分片键值。
您创建的分片键值范围的下边界和上边界不能与分片集合的现有范围重叠。 例如,给定1到10的现有范围,您无法创建5到20的新范围,因为新范围将与现有范围重叠。
一个区域可以有多个与之关联的数据范围,但一个范围最多只能与一个区域关联。
When removing the association between a range and a zone, updateZoneKeyRange does not remove the zone. Use the removeShardFromZone command to remove the association between a zone and a shard.
有关分片集群中区域的更多信息,请参阅区域手册页面。
空集合或不存在集合的初始数据段分布
如果您考虑对空集合或不存在的集合执行区域分片,请在分片集合之前使用updateZoneKeyRange 创建区域和区域范围(从.. 40240开始)。从版本..3 开始,在空集合或不存在的集合上创建区域和区域范围允许MongoDB在对集合分片优化初始数据块创建和分配进程。与在分片后创建区域相比,这种优化的进程支持更快地设置分区分片,并且开销负载均衡器开销更少。负载均衡器在优化初始数据块创建和分配后执行所有数据块管理。
有关为初始数据块分布定义区域和区域范围的示例,请参阅为空的collection或非现有的collection预定义区域和区域范围。
使用复合哈希分片键进行初始数据段分配
MongoDB 支持对复合哈希索引上的集合进行分片。 在复合哈希分片键上对空集合或不存在的集合进行分片时,MongoDB 可以执行优化的初始数据段创建和分配。
如果哈希字段是分片键的前缀(即分片键中的第一个字段),则必须满足以下所有条件,MongoDB 才能执行初始数据段创建和分发:
该集合具有单个区域范围,所有下限字段为
MinKey,所有上限字段为MaxKey。
如果哈希字段不是分片分片键的前缀(即分片分片键具有一个或多个非哈希前导字段),则必须满足以下所有条件, MongoDB才能执行初始数据块创建和分配:
该集合为不同前缀字段值的每个组合(即 哈希字段之前的所有字段)。
对于每个区域范围的下限,请为哈希字段和所有后续字段指定
MinKey。对于每个区域范围,至少有一个上限前缀字段必须与其对应的下限前缀字段不同。
有关在复合哈希分片键上为初始数据块分布定义区域和区域范围的更完整示例,请参阅为空或不存在的collection预定义区域和区域范围。
平衡器
After successfully running updateZoneKeyRange, there may be chunk migrations during the next balancer round.
将范围添加到区域后,负载均衡器必须首先运行,以便将范围被区域覆盖的任何数据段迁移到该区域内的分片。 在均衡完成之前,考虑到为分片集群配置的区域,某些数据块可能驻留在错误的分片上。
删除范围和区域之间的关联会删除在该区域内的分片上保持范围所覆盖的数据段的约束。 在负载均衡器的下一个回合中,负载均衡器可能会迁移该区域之前覆盖的数据块。
有关迁移如何在分片集群中工作的更多信息,请参阅分片集群负载均衡器的文档。
边界
区域范围始终包括下边界,但不包括上边界。
删除的集合
删除集合将删除其关联的区域/标记范围。
在早期版本中,MongoDB 不会删除已删除集合的标签关联,如果您稍后创建同名的新集合,则旧标签关联将应用于新集合。
安全性
对于使用身份验证运行的分片集群,您必须通过以下任一项身份验证:
权限包括对
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.
例子
给定分片键为{ a : 1 }的分片集合exampledb.collection ,以下操作在alpha区域上创建一个下限为1且上限为10的范围:
admin = db.getSiblingDB("admin") admin.runCommand( { updateZoneKeyRange : "exampledb.collection", min : { a : 1 }, max : { a : 10 }, zone : "alpha" } )
以下操作通过将null传递给zone字段来删除之前创建的范围。
admin = db.getSiblingDB("admin") admin.runCommand( { updateZoneKeyRange : "exampledb.collection", min : { a : 1 }, max : { a : 10 }, zone : null } )
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 : 1, b : 1 }的分片集合exampledb.collection ,以下操作创建一个覆盖{ 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" } )