Database Manual / Reference / Database Commands / Sharding

mergeChunks (database command数据库命令)

Definition定义

mergeChunks
For a sharded collection, mergeChunks combines contiguous chunk ranges on a shard into a single chunk. Issue the mergeChunks command on the admin database from a mongos instance.对于分片集合,mergeChunks将分片上的连续范围组合成一个块。从mongos实例对管理数据库发出mergeChunks命令。

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(
{
mergeChunks: <namespace>,
bounds : [
{ <shardKeyField>: <minFieldValue> },
{ <shardKeyField>: <maxFieldValue> }
]
}
)

For compound shard keys, you must include the full shard key in the bounds specification. For example, if the shard key is { x: 1, y: 1 }, mergeChunks has the following form:对于复合分片键,您必须在bounds规范中包含完整的分片键。例如,如果分片键是{ x: 1, y: 1 },那么mergeChunks的形式如下:

db.adminCommand(
{
mergeChunks: <namespace>,
bounds: [
{ x: <minValue>, y: <minValue> },
{ x: <maxValue>, y: <maxValue> }
]
}
)

Command Fields命令字段

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

Field字段Type类型Description描述
mergeChunksnamespace命名空间The fully qualified namespace of the collection where both chunks exist. Namespaces take form of <database>.<collection>.两个块都存在的集合的完全限定命名空间。命名空间采用<database>.<collection>的形式。
boundsarray数组An array that contains the minimum and maximum key values of the new chunk.包含新块的最小和最大键值的数组。

Access Control访问控制

On deployments running with authorization, the built-in role clusterManager provides the required privileges.authorization运行的部署中,内置的角色clusterManager提供所需的权限。

Behavior行为

Note

Use the mergeChunks only in special circumstances. For instance, when cleaning up your sharded cluster after removing many documents.仅在特殊情况下使用mergeChunks。例如,在删除许多文档后清理分片集群时。

In order to successfully merge chunks, the following must be true:为了成功合并块,必须满足以下条件:

  • In the bounds field, <minkey> and <maxkey> must correspond to the lower and upper bounds of the chunks to merge.在边界字段中,<minkey><maxkey>必须对应于要合并的块的下限和上限。
  • The chunks must reside on the same shard.这些块必须位于同一个分片上。
  • The chunks must be contiguous.这些块必须是连续的。

mergeChunks returns an error if these conditions are not satisfied.如果不满足这些条件,则返回错误。

Return Messages返回消息

On success, mergeChunks returns this document:成功后,mergeChunks将返回以下文档:

{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1510767081, 1),
"signature" : {
"hash" : BinData(0,"okKHD0QuzcpbVQg7mP2YFw6lM04="),
"keyId" : Long("6488693018630029321")
}
},
"operationTime" : Timestamp(1510767081, 1)
}

Another Operation in Progress另一项操作正在进行中

mergeChunks returns the following error message if another metadata operation is in progress on the chunks collection:如果chunks集合上正在进行另一个元数据操作,则返回以下错误消息:

errmsg: "The collection's metadata lock is already taken."

If another process, such as balancer process, changes metadata while mergeChunks is running, you may see this error. You can retry the mergeChunks operation without side effects.如果另一个进程(如平衡器进程)在mergeChunks运行时更改元数据,您可能会看到此错误。您可以在没有副作用的情况下重试mergeChunks操作。

Chunks on Different Shards不同分片上的块状物

If the input chunks are not on the same shard, mergeChunks returns an error similar to the following:如果输入不在同一个分片上,mergeChunks将返回类似于以下内容的错误:

{
"ok" : 0,
"errmsg" : "could not merge chunks, collection test.users does not contain a chunk ending at { username: \"user63169\" }",
"$clusterTime" : {
"clusterTime" : Timestamp(1510767081, 1),
"signature" : {
"hash" : BinData(0,"okKHD0QuzcpbVQg7mP2YFw6lM04="),
"keyId" : Long("6488693018630029321")
}
},
"operationTime" : Timestamp(1510767081, 1)
}

Noncontiguous Chunks非连续块

If the input chunks are not contiguous, mergeChunks returns an error similar to the following:如果输入不连续,mergeChunks将返回类似于以下内容的错误:

{
"ok" : 0,
"errmsg" : "could not merge chunks, collection test.users has more than 2 chunks between [{ username: \"user29937\" }, { username: \"user49877\" })"
"$clusterTime" : {
"clusterTime" : Timestamp(1510767081, 1),
"signature" : {
"hash" : BinData(0,"okKHD0QuzcpbVQg7mP2YFw6lM04="),
"keyId" : Long("6488693018630029321")
}
},
"operationTime" : Timestamp(1510767081, 1)

}