mergeAllChunksOnShard
Definition
mergeAllChunksOnShard
mergeAllChunksOnShard
finds and merges all mergeable chunks that a shard owns for a given collection.
Compatibility
This command is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Important
This command is not supported in serverless instances. For more information, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
The command has the following syntax:
db.adminCommand( { mergeAllChunksOnShard: <name of the collection>, shard: <name of the shard>, maxNumberOfChunksToMerge: <maximum number of chunks to merge> /* optional */ } )
Command Fields
The command takes the following fields:
Field | Type | Necessity | Description |
---|---|---|---|
| string | Required | Name of the collection. |
| string | Required | Name of the shard. |
| integer | Optional | Maximum number of chunks to merge. |
Behavior
mergeAllChunksOnShard
finds and merges all mergeable chunks for a
collection on the same shard. Two or more contiguous chunks in the same
collection are mergeable when they meet all of these conditions:
They are owned by the same shard.
They are not jumbo chunks.
jumbo
chunks are not mergeable because they cannot participate in migrations.Their history can be purged safely, without breaking transactions and snapshot reads:
The last migration involving the chunk happened at least as many seconds ago as the value of
minSnapshotHistoryWindowInSeconds
.The last migration involving the chunk happened at least as many seconds ago as the value of
transactionLifetimeLimitSeconds
.
Example
This example assumes that history is empty for all chunks and all chunks are non-jumbo. Since both conditions are true, all contiguous intervals on the same shard are mergeable.
Setup
These chunks belong to a collection named coll
with shard key x
.
There are nine chunks in total.
Chunk ID | Min | Max | Shard |
---|---|---|---|
A |
|
| Shard0 |
B |
|
| Shard0 |
C |
|
| Shard0 |
D |
|
| Shard0 |
E |
|
| Shard1 |
F |
|
| Shard1 |
G |
|
| Shard0 |
H |
|
| Shard0 |
I |
|
| Shard1 |
Steps
Result
After these commands have completed, the contiguous chunks have been merged. There are four total chunks instead of the original nine.
Chunk ID | Min | Max | Shard |
---|---|---|---|
A-B-C-D |
|
| Shard0 |
E-F |
|
| Shard1 |
G-H |
|
| Shard0 |
I |
|
| Shard1 |