Skip to content

Commit 98b6177

Browse files
authored
DOCSP-34846 autoCompact Reference Page (#6361)
1 parent 43d9db7 commit 98b6177

File tree

7 files changed

+185
-15
lines changed

7 files changed

+185
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Optional. Specifies the minimum amount of storage space, in megabytes, that must
2+
be recoverable for compaction to proceed.

source/reference/built-in-roles.txt

+1
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ Cluster Administration Roles
595595

596596
- :authaction:`applicationMessage`
597597
- :authaction:`closeAllDatabases`
598+
- :authaction:`compact` (New in version 7.3)
598599
- :authaction:`connPoolSync`
599600
- :authaction:`flushRouterConfig`
600601
- :authaction:`fsync`
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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`

source/reference/command/compact.txt

+14-12
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ Definition
1717

1818
.. dbcommand:: compact
1919

20-
Rewrites and defragments all data and indexes in a collection. On
21-
:ref:`WiredTiger <storage-wiredtiger>` databases, this command
22-
releases unneeded disk space to the operating system.
20+
Attempts to release unneeded disk space to the operating system.
2321

2422
Syntax
2523
------
@@ -69,10 +67,7 @@ The command takes the following fields:
6967
- Integer
7068
- .. versionadded: 7.3
7169

72-
Optional. Specifies the minimum amount of storage space, in megabytes,
73-
that must be recoverable for compaction to proceed. Compaction proceeds
74-
only if the amount of free storage space available is greater than
75-
``freeSpaceTargetMB``.
70+
.. include:: /includes/fact-freeSpaceTargetMB.rst
7671

7772
*Default:* 20
7873

@@ -92,9 +87,9 @@ The command takes the following fields:
9287

9388
For clusters enforcing :ref:`authentication <authentication>`,
9489
you must authenticate as a user with the :authaction:`compact` privilege
95-
action on the target collection. The :authrole:`dbAdmin` role provides
96-
the required privileges for running ``compact`` against
97-
non-system collections.
90+
action on the target collection. The :authrole:`dbAdmin` and
91+
:authrole:`hostManager` roles provide the required privileges for running
92+
``compact`` against non-system collections.
9893

9994
For :ref:`system collections <metadata-system-collections>`, you must:
10095

@@ -127,8 +122,8 @@ collection:
127122
For more information on configuring the ``resource`` document, see
128123
:ref:`resource-document`.
129124

130-
To add the :authrole:`dbAdmin` or the custom role to an existing
131-
user, use :method:`db.grantRolesToUser` or :method:`db.updateUser()`.
125+
To add the :authrole:`dbAdmin`, :authrole:`hostManager`, or the custom role to
126+
an existing user, use :method:`db.grantRolesToUser` or :method:`db.updateUser()`.
132127
The following operation grants the custom ``compact`` role to the
133128
``myCompactUser`` on the ``admin`` database:
134129

@@ -157,6 +152,8 @@ specify the role to the ``roles`` array of the
157152
Behavior
158153
--------
159154

155+
.. _compact-blocking:
156+
160157
Blocking
161158
~~~~~~~~
162159

@@ -333,3 +330,8 @@ Running ``compact`` returns output similar to the following:
333330
:copyable: false
334331

335332
{ bytesFreed: 27859, ok: 1 }
333+
334+
Learn More
335+
----------
336+
337+
- :dbcommand:`autoCompact`

source/reference/command/nav-administration.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Administration Commands
2020

2121
- Description
2222

23+
* - :dbcommand:`autoCompact`
24+
25+
- Enables background compaction.
26+
27+
.. versionadded:: 8.0
28+
2329
* - :dbcommand:`bulkWrite`
2430

2531
- Performs many insert, update, and delete operations on multiple
@@ -37,7 +43,7 @@ Administration Commands
3743

3844
* - :dbcommand:`compact`
3945

40-
- Defragments a collection and rebuilds the indexes.
46+
- Attempts to release unneeded disk space to the operating system.
4147

4248
* - :dbcommand:`convertToCapped`
4349

@@ -182,6 +188,7 @@ Administration Commands
182188
:titlesonly:
183189
:hidden:
184190

191+
/reference/command/autoCompact
185192
/reference/command/bulkWrite
186193
/reference/command/cloneCollectionAsCapped
187194
/reference/command/collMod

source/reference/privilege-actions.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,8 @@ Server Administration Actions
599599

600600
.. authaction:: compact
601601

602-
User can perform the :dbcommand:`compact` command. Apply this action to
603-
database or collection resources.
602+
User can perform the :dbcommand:`compact` command and :dbcommand:`autoCompact`
603+
commands. Apply this action to database or collection resources.
604604

605605
.. authaction:: compactStructuredEncryptionData
606606

source/release-notes/8.0.txt

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ Release Notes for MongoDB 8.0 (Release Candidate)
1717
General Changes
1818
---------------
1919

20+
Background Compaction
21+
~~~~~~~~~~~~~~~~~~~~~
22+
23+
Starting in MongoDB 8.0, you can use the new :dbcommand:`autoCompact` command
24+
to perform background compaction. If enabled, the server attempts to keep free
25+
space within each collection and index below the specified the
26+
``freeSpaceTargetMB`` value.
27+
2028
New Bulk Write Command
2129
~~~~~~~~~~~~~~~~~~~~~~
2230

0 commit comments

Comments
 (0)