@@ -63,6 +63,8 @@ The command takes the following fields:
63
63
- flag
64
64
- .. versionchanged:: 4.4
65
65
66
+ .. _compact-force-option:
67
+
66
68
Optional. Starting in v4.4, if specified, forces
67
69
:dbcommand:`compact` to run on the :term:`primary` in
68
70
a :term:`replica set`. Before v4.4, this boolean field
@@ -95,30 +97,6 @@ action on the target collection. The :authrole:`dbAdmin` role provides
95
97
the required privileges for running :dbcommand:`compact` against
96
98
non-system collections.
97
99
98
- For :ref:`system collections <metadata-system-collections>`, create a
99
- custom role that grants the :authaction:`compact` action on the system
100
- collection. You can then grant that role to a new or existing user and
101
- authenticate as that user to perform the :dbcommand:`compact` command.
102
- For example, the following operations create a custom role that grants
103
- the :authaction:`compact` action against specified database and
104
- collection:
105
-
106
- .. code-block:: javascript
107
-
108
- use admin
109
- db.createRole(
110
- {
111
- role: "myCustomCompactRole",
112
- privileges: [
113
- {
114
- resource: { "db" : "<database>" , "collection" : "<collection>" },
115
- actions: [ "compact" ]
116
- }
117
- ],
118
- roles: []
119
- }
120
- )
121
-
122
100
For more information on configuring the ``resource`` document, see
123
101
:ref:`resource-document`.
124
102
@@ -155,28 +133,34 @@ Behavior
155
133
Blocking
156
134
~~~~~~~~
157
135
158
- .. versionchanged:: 4.4
136
+ Blocking behavior is version specific.
137
+
138
+ .. list-table::
139
+
140
+ * - Version
141
+ - Blocking Behavior
159
142
160
- Starting in v4.4, on :ref:`WiredTiger <storage-wiredtiger>`,
161
- :dbcommand:` compact` only blocks the following metadata operations:
143
+ * - Pre 4.4
144
+ - `` compact`` blocks all read and write activity.
162
145
163
- - :method:`db.collection.drop`
164
- - :method:`db.collection.createIndex` and
165
- :method:`db.collection.createIndexes`
166
- - :method:`db.collection.dropIndex` and
167
- :method:`db.collection.dropIndexes`
146
+ * - 4.4
147
+ - - .. include:: /includes/reference/compact-methods-list.rst
148
+ - All other operations are permitted.
168
149
169
- :dbcommand:`compact` does not block :ref:`crud` for the database it is
170
- currently operating on.
150
+ * - Post 4.4.17, 5.0.12, 6.0.2
151
+ - - .. include:: /includes/reference/compact-methods-list.rst
152
+ - All other operations are permitted.
153
+ - The :ref:`locking order <faq-concurrency-locking>` changes.
171
154
172
- Before v4.4, :dbcommand:`compact` blocked all operations for the
173
- database it was compacting, including :ref:`crud`, and was therefore
174
- recommended for use only during scheduled maintenance periods. Starting in
175
- v4.4, the :dbcommand:`compact` command is appropriate for use at any time.
155
+ To run ``compact`` in a replica set, see
156
+ :ref:`compact-cmd-replica-sets` for additional considerations.
176
157
177
- You may view the intermediate progress either by viewing the
178
- :binary:`~bin.mongod` log file or by running the
179
- :method:`db.currentOp()` in another shell instance.
158
+ Monitoring Progress
159
+ ~~~~~~~~~~~~~~~~~~~
160
+
161
+ To check the ``compact`` operation's progress, monitor the
162
+ :binary:`~bin.mongod` log file or run :method:`db.currentOp()`
163
+ from another shell instance.
180
164
181
165
Operation Termination
182
166
~~~~~~~~~~~~~~~~~~~~~
@@ -203,45 +187,91 @@ Disk Space
203
187
To see how the storage space changes for the collection, run the
204
188
:dbcommand:`collStats` command before and after compaction.
205
189
206
- On :ref:`WiredTiger <storage-wiredtiger>`, :dbcommand:`compact` attempts to
207
- reduce the required storage space for data and indexes in a collection, releasing
208
- unneeded disk space to the operating system. The effectiveness of this operation
209
- is workload dependent and no disk space may be recovered. This command is useful
210
- if you have removed a large amount of data from the collection, and do not plan
211
- to replace it.
190
+ On :ref:`WiredTiger <storage-wiredtiger>`, :dbcommand:`compact` attempts
191
+ to reduce the required storage space for data and indexes in a
192
+ collection, releasing unneeded disk space to the operating system. The
193
+ effectiveness of this operation is workload dependent and no disk space
194
+ may be recovered. This command is useful if you have removed a large
195
+ amount of data from the collection, and do not plan to replace it.
212
196
213
- :dbcommand:`compact` may require additional disk space to run on WiredTiger databases.
197
+ :dbcommand:`compact` may require additional disk space to run on
198
+ WiredTiger databases.
214
199
215
200
.. _compact-cmd-replica-sets:
216
201
217
202
Replica Sets
218
203
~~~~~~~~~~~~
219
204
220
- :dbcommand:`compact` commands do not replicate to secondaries in a
221
- :term:`replica set`.
205
+ You can use ``compact`` on collections and indexes that are stored in a
206
+ replica set, however there are some important considerations:
207
+
208
+ - The primary node does not replicate the ``compact`` command to the
209
+ secondaries.
210
+ - The ``compact`` command blocks writes while it runs.
211
+ - You should run ``compact`` on secondary nodes whenever possible. If
212
+ you cannot run ``compact`` on secondaries, see the :ref:`force
213
+ <compact-force-option>` option.
214
+ - Starting in MongoDB 6.0.2 (and 5.0.12, and 4.4.17):
215
+
216
+ - A secondary node can replicate while ``compact`` is running.
217
+ - Reads are permitted.
218
+
219
+ To run ``compact`` on a cluster
220
+
221
+ .. procedure::
222
+ :style: connected
223
+
224
+ .. step:: Compact the secondary nodes.
225
+
226
+ Run ``compact`` on one of the secondary nodes. When ``compact``
227
+ finishes, repeat the operation on each of the remaining
228
+ secondaries in turn.
229
+
230
+ .. step:: Reassign the primary node.
231
+
232
+ To step down the current primary and trigger an election, use the
233
+ :method:`rs.stepDown()` method. To nominate a particular secondary
234
+ node, :ref:`adjust the member priority
235
+ <replica-set-adjust-priority>`.
236
+
237
+ .. step:: Compact the old primary.
238
+
239
+ After stepping down, the old primary node becomes a secondary
240
+ node. Run ``compact`` on the old primary node.
241
+
242
+ Version Specific Considerations for Secondary Nodes
243
+ ```````````````````````````````````````````````````
244
+
245
+ Blocking behavior on secondary nodes is version specific.
246
+
247
+ .. list-table::
248
+
249
+ * - Version
250
+ - Blocking Behavior
222
251
223
- - Compact each member separately.
252
+ * - Pre 4.4
253
+ - - ``compact`` blocks all read and write activity.
254
+ - No replication possible.
255
+ - Secondary node is in :replstate:`RECOVERING` state.
224
256
225
- - Ideally run :dbcommand:`compact` on a secondary. See option
226
- ``force`` above for information regarding compacting the primary.
257
+ * - 4.4
258
+ - - ``compact`` blocks all write activity.
259
+ - No replication possible.
260
+ - Reads not permitted.
227
261
228
- - Starting in MongoDB 5.0.3 (and 4.4.9 and 4.2.18): a secondary is
229
- not available when :dbcommand:`compact` is running. The secondary does
230
- not enter the :replstate:`RECOVERING` state.
262
+ * - Post 4.4.17, 5.0.12, 6.0.2
263
+ - - ``compact`` blocks all write activity.
264
+ - No replication possible.
265
+ - Reads permitted.
231
266
232
- - For previous MongoDB versions: on secondaries, :dbcommand:`compact`
233
- forces the secondary to enter the :replstate:`RECOVERING` state. Read
234
- operations issued to an instance in the :replstate:`RECOVERING` state
235
- will fail. This prevents clients from reading during the operation.
236
- When the operation completes, the secondary returns to
237
- :replstate:`SECONDARY` state.
267
+ When ``compact`` completes, the secondary returns to the
268
+ :replstate:`SECONDARY` state.
238
269
239
- See :doc:`/reference/replica-states/` for more information about replica
240
- set member states.
270
+ For more information about replica set member states, see
271
+ See :ref:`replica-set- member- states` .
241
272
242
- See :doc:`/tutorial/perform-maintence-on-replica-set-members` for an
243
- example replica set maintenance procedure to maximize availability
244
- during maintenance operations.
273
+ For replica set maintenance and availability, see
274
+ :ref:`perform-maint-on-replica-set`.
245
275
246
276
Sharded Clusters
247
277
~~~~~~~~~~~~~~~~
0 commit comments