configureCollectionBalancing
On this page
Definition
Syntax
The command has the following syntax:
db.adminCommand( { configureCollectionBalancing: "<collection>", chunkSize: <num>, defragmentCollection: <bool> } )
Command Fields
configureCollectionBalancing
has the following fields:
Field | Type | Description |
---|---|---|
configureCollectionBalancing | string | Required: The name of the database and sharded collection to
configure. |
chunkSize | integer | Optional: Sets the chunk size in MiB for the collection. The
recommended size is 256, 512, or larger. |
defragmentCollection | boolean | Optional: Causes the balancer to defragment the collection. |
For more information, see Data Partitioning with Chunks.
Examples
Configure Chunk Size
To change the chunk size for a sharded collection, use
the chunkSize
option:
db.adminCommand( { configureCollectionBalancing: "test.students", chunkSize: 256 } )
Use this command to change the chunk size for the given collection.
Warning
By default, MongoDB cannot move a chunk if the number of documents in the chunk is greater than 1.3 times the result of dividing the configured chunk size by the average document size.
To find the average document size, see the avgObjSize
field in the
output of the db.collection.stats()
method.
For more information, see Chunk Size.
Defragment Collections
To tell the balancer to defragment a sharded collection, use the
defragmentCollection
option:
db.adminCommand( { configureCollectionBalancing: "test.students", defragmentCollection: true } )
Use this command to have the balancer defragment a sharded collection.
Reconfigure and Defragment Collections
To defragment a sharded collection while updating the chunk size, use
the defragmentCollection
option and the chunkSize
option
together:
db.adminCommand( { configureCollectionBalancing: "test.students", chunkSize: 512, defragmentCollection: true } )