|
| 1 | +=========== |
| 2 | +autoCompact |
| 3 | +=========== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Definition |
| 14 | +---------- |
| 15 | + |
| 16 | +.. dbcommand:: autoCompact |
| 17 | + |
| 18 | + .. versionadded:: 8.0 |
| 19 | + |
| 20 | + Enables or disables background :ref:`compaction <compact>`. |
| 21 | + When enabled, ``autoCompact`` periodically iterates through all available |
| 22 | + files and continuously runs compaction if there is enough free storage space |
| 23 | + available. |
| 24 | + |
| 25 | + Before you enable ``autoCompact``, run the :dbcommand:`dbStats` command to |
| 26 | + see if you have enough available storage space for compaction to proceed. If |
| 27 | + the amount of available space returned by ``dbStats`` is less than |
| 28 | + ``freeSpaceTargetMB``, background compaction has no effect. |
| 29 | + |
| 30 | +Syntax |
| 31 | +------ |
| 32 | + |
| 33 | +The command has the following syntax: |
| 34 | + |
| 35 | +.. code-block:: javascript |
| 36 | + |
| 37 | + db.runCommand( |
| 38 | + { |
| 39 | + autoCompact: <boolean>, |
| 40 | + freeSpaceTargetMB: <int>, // Optional |
| 41 | + runOnce: <boolean>, // Optional |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | +Compatibility |
| 46 | +------------- |
| 47 | + |
| 48 | +This command is available in deployments hosted in the following environments: |
| 49 | + |
| 50 | +.. include:: /includes/fact-environments-atlas-only.rst |
| 51 | + |
| 52 | +.. include:: /includes/fact-environments-atlas-support-no-free.rst |
| 53 | + |
| 54 | +.. include:: /includes/fact-environments-onprem-only.rst |
| 55 | + |
| 56 | +Command Fields |
| 57 | +-------------- |
| 58 | + |
| 59 | +The command can take the following optional fields: |
| 60 | + |
| 61 | +.. list-table:: |
| 62 | + :header-rows: 1 |
| 63 | + :widths: 20 20 80 |
| 64 | + |
| 65 | + * - Field |
| 66 | + - Type |
| 67 | + - Description |
| 68 | + |
| 69 | + * - ``freeSpaceTargetMB`` |
| 70 | + - Integer |
| 71 | + - .. versionadded: 7.3 |
| 72 | + |
| 73 | + .. include:: /includes/fact-freeSpaceTargetMB.rst |
| 74 | + |
| 75 | + *Default:* 20 |
| 76 | + |
| 77 | + * - ``runOnce`` |
| 78 | + - boolean |
| 79 | + - Optional. If ``runOnce`` is set to ``true``, background compaction runs |
| 80 | + only once through every collection on the node. |
| 81 | + |
| 82 | + If ``runOnce`` is set to ``false``, background compaction runs |
| 83 | + continuously on all collections in the database. If a collection fails to |
| 84 | + compact while ``runOnce`` is ``false``, MongoDB temporarily skips that |
| 85 | + collection and continues compacting the remaining collections. MongoDB |
| 86 | + attempts to compact the failed collection again after approximately one |
| 87 | + day. |
| 88 | + |
| 89 | +.. warning:: |
| 90 | + |
| 91 | + Always have an up-to-date backup before performing server |
| 92 | + maintenance such as the ``autoCompact`` operation. |
| 93 | + |
| 94 | +.. _autocompact-authentication: |
| 95 | + |
| 96 | +Required Privileges |
| 97 | +------------------- |
| 98 | + |
| 99 | +For clusters enforcing :ref:`authentication <authentication>`, |
| 100 | +you must authenticate as a user with the :authaction:`compact` privilege |
| 101 | +action on the target collection. The :authrole:`dbAdmin` and |
| 102 | +:authrole:`hostManager` roles provide the required privileges for running |
| 103 | +``autoCompact`` against all collections. |
| 104 | + |
| 105 | +Behavior |
| 106 | +-------- |
| 107 | + |
| 108 | +Blocking |
| 109 | +~~~~~~~~ |
| 110 | + |
| 111 | +Although the ``autoCompact`` command itself doesn't block any reads and |
| 112 | +writes, background compaction applies the same :ref:`blocking behavior |
| 113 | +<compact-blocking>` as the :dbcommand:`compact` command. |
| 114 | + |
| 115 | +Excluded Collections |
| 116 | +~~~~~~~~~~~~~~~~~~~~ |
| 117 | + |
| 118 | +If an :term:`oplog` exists, MongoDB excludes it from background compaction. |
| 119 | + |
| 120 | +Performance Considerations |
| 121 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 122 | + |
| 123 | +We recommend running ``autoCompact`` during periods of low traffic. If you run |
| 124 | +background compaction in parallel with other operations, it can negatively |
| 125 | +impact performance. |
| 126 | + |
| 127 | +Replica Sets |
| 128 | +~~~~~~~~~~~~ |
| 129 | + |
| 130 | +You can run background compaction on collections and indexes that are |
| 131 | +stored in a replica set. However, note the following considerations: |
| 132 | + |
| 133 | +- The primary node does not replicate the ``autoCompact`` command to the |
| 134 | + secondary nodes. |
| 135 | +- A secondary node can replicate data while background compaction is |
| 136 | + running. |
| 137 | +- Reads and writes are permitted while background compaction is running. |
| 138 | + |
| 139 | +Sharded Clusters |
| 140 | +~~~~~~~~~~~~~~~~ |
| 141 | + |
| 142 | +``autoCompact`` only applies to :binary:`~bin.mongod` instances. In a |
| 143 | +sharded environment, run ``autoCompact`` on each shard separately. |
| 144 | + |
| 145 | +You cannot run ``autoCompact`` against a :binary:`~bin.mongos` instance. |
| 146 | + |
| 147 | +Learn More |
| 148 | +---------- |
| 149 | + |
| 150 | +- :dbcommand:`compact` |
0 commit comments