Definition
compactRewrites and defragments all data and indexes in a collection. On WiredTiger databases, this command will release unneeded disk space to the operating system.
Compatibility
This command is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Important
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
The command has the following syntax:
db.runCommand( { compact: <collection name> } )
Command Fields
The command takes the following fields:
Note
Starting in MongoDB 4.2
MongoDB removes the MMAPv1 storage engine and the MMAPv1 specific options paddingFactor, paddingBytes, preservePadding for compact.
Field | Type | Description |
|---|---|---|
| string | The name of the collection. |
| flag | Optional. Starting in v4.4, if specified, forces Starting in v4.4, |
| 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:
A comment can be any valid BSON type (string, integer, object, array, etc). |
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 more information on configuring the resource document, see Resource Document on Self-Managed Deployments.
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:
use admin db.grantRolesToUser("myCompactUser", [ "dbAdmin" | "myCustomCompactRole" ] )
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.
use admin db.createUser( { user: "myCompactUser", pwd: "myCompactUserPassword", roles: [ { role: "dbAdmin", db: "<database>" } | "myCustomCompactRole" ] } )
Behavior
Monitoring Progress
To check the compact operation's progress, monitor the mongod log file or run db.currentOp() from another shell instance.
Operation Termination
If you terminate compact with the db.killOp() method or restart the server before the operation finishes, compact ends and may fail its attempt to release disk space back to the operating system.
Disk Space
The compact command attempts to reduce the disk space consumed for data and indexes in a collection by releasing obsolete blocks back to the operating system. The effectiveness of compact is relative to how many blocks are available to be released and where in the data file the blocks are.
To see how the storage space changes for the collection, run the collStats command before and after compaction. You can use the output metric collStats.freeStorageSize to view the amount of storage available for reuse.
The operation is iterative and operates on segments of the data file in each pass. To view an estimate of how much space compact will release, use the dryRun flag. Calling compact on a collection will compact both the collection and its associated indexes.
compact may require additional disk space to run.
Replica Sets
You can use compact on collections and indexes that are stored in a replica set, however there are some important considerations:
The primary node does not replicate the
compactcommand to the secondaries.You should run
compacton secondary nodes whenever possible. If you cannot runcompacton secondaries, see the force option.Starting in MongoDB 6.1.0 (and 6.0.2):
A secondary node can replicate while
compactis running.Reads are permitted.
To run compact on a cluster
Reassign the primary node.
To step down the current primary and trigger an election, use the rs.stepDown() method. To nominate a particular secondary node, adjust the member priority.
Version Specific Considerations for Secondary Nodes
Blocking behavior on secondary nodes is version specific.
Version | Blocking Behavior |
4.4 |
|
Post 4.4.17, 5.0.12, and 6.0.2 |
|
While the compact command is running, the replica set remains in a SECONDARY status.
For more information about replica set member states, see See Replica Set Member States.
For replica set maintenance and availability, see Perform Maintenance on Self-Managed Replica Set Members.
Sharded Clusters
compact only applies to mongod instances. In a sharded environment, run compact on each shard separately as a maintenance operation.
Index Building
mongod rebuilds all indexes in parallel following the compact operation.