- Reference >
- Database Commands >
- compact
compact¶
-
compact¶ New in version 2.0.
The
compactcommand rewrites and defragments a single collection. Additionally, the command drops all indexes at the beginning of compaction and rebuilds the indexes at the end.compactis conceptually similar torepairDatabase, but works on a single collection rather than an entire database.The command has the following syntax:
You may also specify the following options:
Parameters: - force –
Changed in version 2.2:
compactblocks activities only for the database it is compacting.The
forcespecifies whether thecompactcommand can run on the primary node in a replica set. Set totrueto run thecompactcommand on the primary node in a replica set. Otherwise, thecompactcommand returns an error when invoked on a replica set primary because the command blocks all other activity. - paddingFactor –
New in version 2.2.
Default:
1.0Minimum:
1.0(no padding.)Maximum:
4.0The
paddingFactordescribes the record size allocated for each document as a factor of the document size, for all records compacted during thecompactoperation.paddingFactordoes not affect the padding of subsequent record allocations aftercompactcompletes.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 subtracting1from thepaddingFactor:For example, a
paddingFactorof1.0specifies a padding size of0whereas apaddingFactorof1.2specifies a padding size of0.2or 20 percent (20%) of the document size.With the following command, you can use the
paddingFactoroption of thecompactcommand to set the record size to1.1of the document size, or a padding factor of 10 percent (10%):compactcompacts existing documents, but does not resetpaddingFactorstatistics for the collection. After thecompactMongoDB will use the existingpaddingFactorwhen allocating new records for documents in this collection. - paddingBytes –
New in version 2.2.
The
paddingBytessets the padding as an absolute number of bytes, for all records compacted during thecompactoperation. After runningcompact,paddingBytesdoes not affect the padding of subsequent record allocations.Specifying
paddingBytescan 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, usingpaddingBytes: 1024might be reasonable since usingpaddingFactor: 4.0would specify a record size of 160 bytes (4.0times 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
paddingBytesoption of thecompactcommand to set the padding size to100bytes on the collection named by<collection>:
Warning
Always have an up-to-date backup before performing server maintenance such as the
compactoperation.Note the following behaviors:
compactblocks all other activity. In MongoDB 2.2,compactblocks activities only for its database. You may view the intermediate progress either by viewing themongodlog file, or by running thedb.currentOp()in another shell instance.compactcompacts existing documents in the collection. However, unlikerepairDatabase,compactdoes not resetpaddingFactorstatistics for the collection. MongoDB will use the existingpaddingFactorwhen allocating new records for documents in this collection.compactgenerally uses less disk space thanrepairDatabaseand is faster. However,thecompactcommand is still slow and does block other database use. Only usecompactduring scheduled maintenance periods.If you terminate the operation with the
db.killOp()method or restart the server before it has finished:- If you have journaling enabled, the data remains consistent and
usable, regardless of the state of the
compactoperation. You may have to manually rebuild the indexes. - If you do not have journaling enabled and the
mongodorcompactterminates during the operation, it’s impossible to guarantee that the data is in a consistent 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.
- If you have journaling enabled, the data remains consistent and
usable, regardless of the state of the
compactmay increase the total size and number of our 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.compactrequires a small amount of additional disk space while running but unlikerepairDatabaseit does not free space on the file system.You may also wish to run the
collStatscommand before and after compaction to see how the storage space changes for the collection.compactcommands do not replicate to secondaries in a replica set:- Compact each member separately.
- Ideally, compaction runs on a secondary. See option
force:trueabove for information regarding compacting the primary.
Warning
If you run
compacton a secondary, the secondary will enter aRECOVERINGstate to prevent clients from sending read operations during compaction. Once the operation finishes the secondary will automatically return toSECONDARYstate. Seestatefor more information replica set member states. Refer to the “partial script for automating step down and compaction” for an example of this procedure.compactis a command issued to amongod. In a sharded environment, runcompacton each shard separately as a maintenance operation.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 collections are not subject to fragmentation.
compactremoves extra padding added to collections that have theusePowerOf2Sizesflag set. See SERVER-4018 for more information.
See also
- force –