mergeChunks
Definition
mergeChunks
- For a sharded collection,
mergeChunks
combinescontiguous chunk ranges on a shard into a singlechunk. Issue themergeChunks
command on theadmin
database from amongos
instance.
Syntax
mergeChunks
has the following form:
- db.adminCommand( { mergeChunks : <namespace> ,
- bounds : [ { <shardKeyField>: <minFieldValue> },
- { <shardKeyField>: <maxFieldValue> } ] } )
For compound shard keys, you must include the full shard key in thebounds
specification. For example, if the shard key is { x: 1, y:1 }
, mergeChunks
has the following form:
- db.adminCommand( { mergeChunks : <namespace> ,
- bounds : [ { x: <minValue>, y: <minValue> },
- { x: <maxValue>, y: <maxValue> } ] } )
Command Fields
The mergeChunks
command takes the following fields:
Field | Type | Description |
---|---|---|
mergeChunks | namespace | The fully qualified namespace of the collectionwhere both chunks exist. Namespaces take form of<database>.<collection> . |
bounds | array | An array that contains the minimum and maximum key values of the newchunk. |
Access Control
On deployments running with authorization
, theuser must have access that includes the following privileges:
splitChunk
action on the collection.
The built-in role clusterManager
provides the requiredprivileges.
Behavior
Note
Use the mergeChunks
only in special circumstances. Forinstance, when cleaning up your sharded cluster after removingmany documents.
In order to successfully merge chunks, the following must be true:
- In the
bounds
field,<minkey>
and<maxkey>
must correspond tothe lower and upper bounds of the chunks to merge. - The chunks must reside on the same shard.
- The chunks must be contiguous.
mergeChunks
returns an error if these conditions are notsatisfied.
Return Messages
On success, mergeChunks
returns to following document:
- {
- "ok" : 1,
- "$clusterTime" : {
- "clusterTime" : Timestamp(1510767081, 1),
- "signature" : {
- "hash" : BinData(0,"okKHD0QuzcpbVQg7mP2YFw6lM04="),
- "keyId" : NumberLong("6488693018630029321")
- }
- },
- "operationTime" : Timestamp(1510767081, 1)
- }
Another Operation in Progress
mergeChunks
returns the following error message if anothermetadata operation is in progress on the chunks
collection:
- errmsg: "The collection's metadata lock is already taken."
If another process, such as balancer process, changes metadata whilemergeChunks
is running, you may see this error. You canretry the mergeChunks
operation without side effects.
Chunks on Different Shards
If the input chunks are not on the same shard,mergeChunks
returns an error similar to the following:
- {
- "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" : NumberLong("6488693018630029321")
- }
- },
- "operationTime" : Timestamp(1510767081, 1)
- }
Noncontiguous Chunks
If the input chunks are not contiguous,mergeChunks
returns an error similar to the following:
- {
- "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" : NumberLong("6488693018630029321")
- }
- },
- "operationTime" : Timestamp(1510767081, 1)
- }