Navigation

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: <collection name> }

compact takes the following fields:

Starting in MongoDB 4.2

MongoDB removes the MMAPv1 storage engine and the MMAPv1 specific options paddingFactor, paddingBytes, preservePadding for compact.

Field Type Description
compact string The name of the collection.
force boolean

Optional. If true, compact can run on the primary in a replica set. If false, compact returns an error when run on a primary, because the command blocks all other operations.

compact blocks operations only for the database it is compacting.

Warning

Always have an up-to-date backup before performing server maintenance such as the compact operation.

Behavior

Blocking

compact only blocks operations for the database it is currently operating on. Only use compact during scheduled maintenance periods.

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 or compact 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 option force:true above for information regarding compacting the primary.
  • On secondaries, the compact command forces the secondary to enter RECOVERING state. Read operations issued to an instance in the RECOVERING state will fail. This prevents clients from reading during the operation. When the operation completes, the secondary returns to SECONDARY state.
  • See Replica Set Member States for more information about replica set member states.

See Perform Maintenance on Replica Set Members for an example replica set maintenance procedure to maximize availability during maintenance operations.

Sharded Clusters

compact only applies to mongod instances. In a sharded environment, run compact on each shard separately as a maintenance operation.

You cannot issue compact against a mongos instance.

Capped Collections

On WiredTiger, the compact command will attempt to compact the collection.

Index Building

New in version 2.6.

mongod rebuilds all indexes in parallel following the compact operation.

←   collMod connPoolSync  →