对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

updateZoneKeyRange(数据库命令)

updateZoneKeyRange

The updateZoneKeyRange administrative command can either create or remove the association between a range of shard key values and a zone.

You can run updateZoneKeyRange database command and its helpers sh.updateZoneKeyRange() and sh.addTagRange() on an unsharded collection or a non-existing collection.

提示

In mongosh, this command can also be run through the sh.updateZoneKeyRange() helper method.

Helper methods are convenient for mongosh users, 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 部署的完全托管服务

该命令具有以下语法:

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

该命令接受以下字段:

Parameter
类型
说明

updateZoneKeyRange

字符串

与范围关联的collection的命名空间。

必须对collection进行分片才能使命令成功。

min

文档

片键值范围的下限(含)。

<fieldname> : <value>的形式指定分片键的每个字段。 该值必须与分片键具有相同的 BSON 类型。

要使用哈希分片, min的字段值需要为NumberLong类型。

max

文档

片键值范围的独占上限。

<fieldname> : <value>的形式指定分片键的每个字段。 该值必须与分片键具有相同的 BSON 类型。

要使用哈希分片, max的字段值需要为NumberLong类型。

zone

字符串

minmax所界定的范围关联的区域名称。

如果该值与现有区域不匹配,则命令失败。

null指定 可删除下限为min 且上限为max updateZoneKeyRange的范围与collection之间的关联。minmax的值必须与目标范围完全匹配。

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 提供了两种辅助方法:

您创建的分片键值范围的下边界和上边界不能与分片集合的现有范围重叠。 例如,给定110的现有范围,您无法创建520的新范围,因为新范围将与现有范围重叠。

一个区域可以有多个与之关联的数据范围,但一个范围最多只能与一个区域关联。

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 才能执行初始数据段创建和分发:

如果哈希字段不是分片分片键的前缀(即分片分片键具有一个或多个非哈希前导字段),则必须满足以下所有条件, MongoDB才能执行初始数据块创建和分配:

  • 该集合为不同前缀字段值的每个组合(即 哈希字段之前的所有字段)。

  • 对于每个区域范围的下限,请为哈希字段和所有后续字段指定MinKey

  • 对于每个区域范围,至少有一个上限前缀字段必须与其对应的下限前缀字段不同。

有关在复合哈希分片键上为初始数据块分布定义区域和区域范围的更完整示例,请参阅为空或不存在的collection预定义区域和区域范围。

After successfully running updateZoneKeyRange, there may be chunk migrations during the next balancer round.

将范围添加到区域后,负载均衡器必须首先运行,以便将范围被区域覆盖的任何数据段迁移到该区域内的分片。 在均衡完成之前,考虑到为分片集群配置的区域,某些数据块可能驻留在错误的分片上。

删除范围和区域之间的关联会删除在该区域内的分片上保持范围所覆盖的数据段的约束。 在负载均衡器的下一个回合中,负载均衡器可能会迁移该区域之前覆盖的数据块。

有关迁移如何在分片集群中工作的更多信息,请参阅分片集群负载均衡器的文档。

区域范围始终包括下边界,但不包括上边界。

删除集合将删除其关联的区域/标记范围。

在早期版本中,MongoDB 不会删除已删除集合的标签关联,如果您稍后创建同名的新集合,则旧标签关联将应用于新集合。

对于使用身份验证运行的分片集群,您必须通过以下任一项身份验证:

  • 其权限包括对config数据库中的各种集合执行指定操作的用户:

    或者,

  • 权限包括对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
}
)

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 : 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"
}
)