Navigation
This version of the documentation is archived and no longer supported.

mergeChunks

Definition

mergeChunks

For a sharded collection, mergeChunks combines two contiguous chunk ranges the same shard into a single chunk. At least one of chunk must not have any documents. Issue the mergeChunks command from a mongos instance.

mergeChunks has the following form:

db.runCommand( { mergeChunks : <namespace> ,
                 bounds : [ { <shardKeyField>: <minFieldValue> },
                            { <shardKeyField>: <maxFieldValue> } ] } )

For compound shard keys, you must include the full shard key in the bounds specification. If the shard key is { x: 1, y: 1 }, mergeChunks has the following form:

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

The mergeChunks command has the following fields:

Field Type Description
mergeChunks namespace The fully qualified namespace of the collection where both chunks exist. Namespaces take form of <database>.<collection>.
bounds array An array that contains the minimum and maximum key values of the new chunk.

Behavior

Note

Use the mergeChunks only in special circumstances such as cleaning up your sharded cluster after removing many documents.

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.
  • The two chunks must reside on the same shard.
  • The two chunks must be contiguous.
  • One or both chunks must be empty.

mergeChunks returns an error if these conditions are not satisfied.

Return Messages

On success, mergeChunks returns to following document:

{ "ok" : 1 }

Another Operation in Progress

mergeChunks returns the following error message if another metadata 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 while mergeChunks is running, you may see this error. You can retry 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\" }"
}

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

Documents in Both Chunks

If neither input chunk is empty, mergeChunks returns an error similar to the following:

{
   "ok" : 0,
   "errmsg" : "could not merge chunks, collection test.users has more than one non-empty chunk between [{ username: \"user36583\" }, { username: \"user49877\" })"
}