|
| 1 | +================================ |
| 2 | +configureCollectionBalancing |
| 3 | +================================ |
| 4 | + |
| 5 | +.. contents:: On this page |
| 6 | + :local: |
| 7 | + :backlinks: none |
| 8 | + :depth: 1 |
| 9 | + :class: singlecol |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :description: configureCollectionBalancing |
| 13 | + :keywords: configureCollectionBalancing |
| 14 | + |
| 15 | +Definition |
| 16 | +------------ |
| 17 | + |
| 18 | +.. dbcommand:: configureCollectionBalancing |
| 19 | + |
| 20 | + .. versionadded:: 5.3 |
| 21 | + |
| 22 | + Configures balancer settings for a sharded collection, such as |
| 23 | + setting the chunk size for and defragmenting the collection. |
| 24 | + |
| 25 | + To run :dbcommand:`configureCollectionBalancing`, use the |
| 26 | + :method:`db.adminCommand` method: |
| 27 | + |
| 28 | + .. code-block:: none |
| 29 | + |
| 30 | + db.adminCommand( { |
| 31 | + configureCollectionBalancing: "<collection>", |
| 32 | + chunkSize: <num>, |
| 33 | + defragmentCollection: <bool> |
| 34 | + } ) |
| 35 | + |
| 36 | + :dbcommand:`configureCollectionBalancing` has the following fields: |
| 37 | + |
| 38 | + .. list-table:: |
| 39 | + :header-rows: 1 |
| 40 | + :widths: 20 20 80 |
| 41 | + |
| 42 | + * - Field |
| 43 | + - Type |
| 44 | + - Description |
| 45 | + * - ``configureCollectionBalancing`` |
| 46 | + - string |
| 47 | + - Required: The name of the database and sharded collection to configure. |
| 48 | + * - ``chunkSize`` |
| 49 | + - integer |
| 50 | + - Optional: Sets the chunk size in MiB for the collection. |
| 51 | + * - ``defragmentCollection`` |
| 52 | + - boolean |
| 53 | + - Optional: Causes the balancer to defragment the collection. |
| 54 | + |
| 55 | + |
| 56 | +For more information, see :ref:`Data Partioning with Chunks |
| 57 | +<sharding-data-partitioning>`. |
| 58 | + |
| 59 | +Examples |
| 60 | +---------- |
| 61 | + |
| 62 | +Configure Chunk Size |
| 63 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +To change the chunk size for a sharded collection, use |
| 66 | +the ``chunkSize`` option: |
| 67 | + |
| 68 | +.. code-block:: javascript |
| 69 | + |
| 70 | + db.adminCommand( { |
| 71 | + configureCollectionBalancing: "test.students", |
| 72 | + chunkSize: 10 |
| 73 | + } ) |
| 74 | + |
| 75 | +Use this command to change the chunk size for the given collection. |
| 76 | + |
| 77 | +.. warning:: |
| 78 | + |
| 79 | + By default, MongoDB cannot move a chunk if the number of documents in |
| 80 | + the chunk is greater than 1.3 times the result of dividing the |
| 81 | + configured chunk size by the average document size. |
| 82 | + |
| 83 | + To find the average document size, see the ``avgObjSize`` field in the |
| 84 | + output of the :method:`db.collection.stats()` method. |
| 85 | + |
| 86 | +For more information, see :ref:`Chunk Size <sharding-chunk-size>`. |
| 87 | + |
| 88 | +Defragment Collections |
| 89 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 90 | + |
| 91 | +To tell the balancer to defragment a sharded collection, use the |
| 92 | +``defragmentCollection`` option: |
| 93 | + |
| 94 | +.. code-block:: javascript |
| 95 | + |
| 96 | + db.adminCommand( { |
| 97 | + configureCollectionBalancing: "test.students", |
| 98 | + defragmentCollection: true |
| 99 | + } ) |
| 100 | + |
| 101 | +Use this command to have the balancer defragment a sharded collection. |
0 commit comments