- Reference >
- Database Commands >
- Administration Commands >
- compact
compact¶
On this page
Definition¶
-
compact
¶ Rewrites and defragments all data and indexes in a collection. On WiredTiger databases, this command will release unneeded disk space to the operating system.
compact
has the following form:compact
takes the following fields:Starting in MongoDB 4.2
MongoDB removes the MMAPv1 storage engine and the MMAPv1 specific options
paddingFactor
,paddingBytes
,preservePadding
forcompact
.Field Type Description compact
string The name of the collection. force
flag Changed in version 4.4.
Optional. Starting in v4.4, if specified, forces
compact
to run on the primary in a replica set. Before v4.4, this boolean field enabledcompact
to run on the primary in a replica set if the value wastrue
and returned an error when run on a primary if the value wasfalse
, because the command blocked all other operations.Starting in v4.4,
compact
does not block MongoDB CRUD Operations on the database it is compacting.comment
any Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
- mongod log messages, in the
attr.command.cursor.comment
field. - Database profiler output, in the
command.comment
field. currentOp
output, in thecommand.comment
field.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
- mongod log messages, in the
Warning
Always have an up-to-date backup before performing server
maintenance such as the compact
operation.
compact
Required Privileges¶
For clusters enforcing authentication,
you must authenticate as a user with the compact
privilege
action on the target collection. The dbAdmin
role provides
the required privileges for running compact
against
non-system collections.
For system collections, create a
custom role that grants the compact
action on the system
collection. You can then grant that role to a new or existing user and
authenticate as that user to perform the compact
command.
For example, the following operations create a custom role that grants
the compact
action against specified database and
collection:
For more information on configuring the resource
document, see
Resource Document.
To add the dbAdmin
or the custom role to an existing
user, use db.grantRolesToUser
or db.updateUser()
.
The following operation grants the custom compact
role to the
myCompactUser
on the admin
database:
To add the dbAdmin
or the custom role to a new user,
specify the role to the roles
array of the
db.createUser()
method when creating the user.
Behavior¶
Blocking¶
Changed in version 4.4.
Starting in v4.4, on WiredTiger,
compact
only blocks the following metadata operations:
db.collection.drop
db.collection.createIndex
anddb.collection.createIndexes
db.collection.dropIndex
anddb.collection.dropIndexes
compact
does not block MongoDB CRUD Operations for the database it is
currently operating on.
Before v4.4, compact
blocked all operations for the
database it was compacting, including MongoDB CRUD Operations, and was therefore
recommended for use only during scheduled maintenance periods. Starting in
v4.4, the compact
command is appropriate for use at any time.
You may view the intermediate progress either by viewing the
mongod
log file or by running the
db.currentOp()
in another shell instance.
Operation Termination¶
If you terminate the operation with the db.killOp()
method or restart the server before the
compact
operation has finished, be aware of the following:
- If you have journaling enabled, the data remains valid and
usable, regardless of the state of the
compact
operation. You may have to manually rebuild the indexes. - If you do not have journaling enabled and the
mongod
orcompact
terminates during the operation, it is impossible to guarantee that the data is in a valid state. - In either case, much of the existing free space in the collection may become un-reusable. In this scenario, you should rerun the compaction to completion to restore the use of this free space.
Disk Space¶
To see how the storage space changes for the collection, run the
collStats
command before and after compaction.
On WiredTiger, compact
attempts to
reduce the required storage space for data and indexes in a collection, releasing
unneeded disk space to the operating system. The effectiveness of this operation
is workload dependent and no disk space may be recovered. This command is useful
if you have removed a large amount of data from the collection, and do not plan
to replace it.
compact
may require additional disk space to run on WiredTiger databases.
Replica Sets¶
compact
commands do not replicate to secondaries in a
replica set.
- Compact each member separately.
- Ideally run
compact
on a secondary. See optionforce
above for information regarding compacting the primary.
Before v4.4, compact
forced the secondary to enter
RECOVERING
state during its execution, which caused read
operations from clients to fail. Starting in v4.4, compact
does not change the run state of the secondary, and clients may continue
to read from the secondary during the compaction operation.
Sharded Clusters¶
compact
only applies to mongod
instances. In a
sharded environment, run compact
on each shard separately
as a maintenance operation.
Capped Collections¶
On WiredTiger, the compact
command will attempt to compact the collection.