Navigation
This version of the documentation is archived and no longer supported.

compact

On this page

Definition

compact

New in version 2.0.

Rewrites and defragments all data in a collection, as well as all of the indexes on that collection. compact has the following form:

{ compact: <collection name> }

compact has the following fields:

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 activity. Beginning with version 2.2, compact blocks activity only for the database it is compacting.
paddingFactor number Optional. Describes the record size allocated for each document as a factor of the document size for all records compacted during the compact operation. The paddingFactor does not affect the padding of subsequent record allocations after compact completes. For more information, see paddingFactor.
paddingBytes integer Optional. Sets the padding as an absolute number of bytes for all records compacted during the compact operation. After compact completes, paddingBytes does not affect the padding of subsequent record allocations. For more information, see paddingBytes.

compact is similar to repairDatabase; however, repairDatabase operates on an entire database.

Warning

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

paddingFactor

New in version 2.2.

The paddingFactor field takes the following range of values:

  • Default: 1.0
  • Minimum: 1.0 (no padding)
  • Maximum: 4.0

If your updates increase the size of the documents, padding will increase the amount of space allocated to each document and avoid expensive document relocation operations within the data files.

You can calculate the padding size by subtracting the document size from the record size or, in terms of the paddingFactor, by subtracting 1 from the paddingFactor:

padding size = (paddingFactor - 1) * <document size>.

For example, a paddingFactor of 1.0 specifies a padding size of 0 whereas a paddingFactor of 1.2 specifies a padding size of 0.2 or 20 percent (20%) of the document size.

With the following command, you can use the paddingFactor option of the compact command to set the record size to 1.1 of the document size, or a padding factor of 10 percent (10%):

db.runCommand ( { compact: '<collection>', paddingFactor: 1.1 } )

compact compacts existing documents but does not reset paddingFactor statistics for the collection. After the compact MongoDB will use the existing paddingFactor when allocating new records for documents in this collection.

paddingBytes

New in version 2.2.

Specifying paddingBytes can be useful if your documents start small but then increase in size significantly. For example, if your documents are initially 40 bytes long and you grow them by 1KB, using paddingBytes: 1024 might be reasonable since using paddingFactor: 4.0 would specify a record size of 160 bytes (4.0 times the initial document size), which would only provide a padding of 120 bytes (i.e. record size of 160 bytes minus the document size).

With the following command, you can use the paddingBytes option of the compact command to set the padding size to 100 bytes on the collection named by <collection>:

db.runCommand ( { compact: '<collection>', paddingBytes: 100 } )

Behaviors

Blocking

In MongoDB 2.2, compact blocks activities only for its database. Prior to 2.2, the command blocked all activities.

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:

  • 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

compact generally uses less disk space than repairDatabase and is faster. However, the compact command is still slow and blocks other database use. Only use compact during scheduled maintenance periods.

compact requires up to 2 gigabytes of additional disk space while running. Unlike repairDatabase, compact does not free space on the file system.

To see how the storage space changes for the collection, run the collStats command before and after compaction.

Size and Number of Data Files

compact may increase the total size and number of your data files, especially when run for the first time. However, this will not increase the total collection storage space since storage size is the amount of data allocated within the database files, and not the size/number of the files on the file system.

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 is a command issued to a mongod. In a sharded environment, run compact on each shard separately as a maintenance operation.

You cannot issue compact against a mongos instance.

Capped Collections

It is not possible to compact capped collections because they don’t have padding, and documents cannot grow in these collections. However, the documents of a capped collection are not subject to fragmentation.

Index Building

New in version 2.6.

mongod rebuilds all indexes in parallel following the compact operation.

←   connectionStatus collMod  →