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