- Reference >
- Database Commands >
- compact
compact¶
-
compact
¶ New in version 2.0.
The
compact
command rewrites and defragments a single collection. Additionally, the command drops all indexes at the beginning of compaction and rebuilds the indexes at the end.compact
is 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:
compact
blocks activities only for the database it is compacting.The
force
specifies whether thecompact
command can run on the primary node in a replica set. Set totrue
to run thecompact
command on the primary node in a replica set. Otherwise, thecompact
command 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.0
Minimum:
1.0
(no padding.)Maximum:
4.0
The
paddingFactor
describes the record size allocated for each document as a factor of the document size, for all records compacted during thecompact
operation.paddingFactor
does not affect the padding of subsequent record allocations aftercompact
completes.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 subtracting1
from thepaddingFactor
:For example, a
paddingFactor
of1.0
specifies a padding size of0
whereas apaddingFactor
of1.2
specifies a padding size of0.2
or 20 percent (20%) of the document size.With the following command, you can use the
paddingFactor
option of thecompact
command to set the record size to1.1
of the document size, or a padding factor of 10 percent (10%):compact
compacts existing documents, but does not resetpaddingFactor
statistics for the collection. After thecompact
MongoDB will use the existingpaddingFactor
when allocating new records for documents in this collection. - paddingBytes –
New in version 2.2.
The
paddingBytes
sets the padding as an absolute number of bytes, for all records compacted during thecompact
operation. After runningcompact
,paddingBytes
does not affect the padding of subsequent record allocations.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, usingpaddingBytes: 1024
might be reasonable since usingpaddingFactor: 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 thecompact
command to set the padding size to100
bytes on the collection named by<collection>
:
Warning
Always have an up-to-date backup before performing server maintenance such as the
compact
operation.Note the following behaviors:
compact
blocks all other activity. In MongoDB 2.2,compact
blocks activities only for its database. You may view the intermediate progress either by viewing themongod
log file, or by running thedb.currentOp()
in another shell instance.compact
compacts existing documents in the collection. However, unlikerepairDatabase
,compact
does not resetpaddingFactor
statistics for the collection. MongoDB will use the existingpaddingFactor
when allocating new records for documents in this collection.compact
generally uses less disk space thanrepairDatabase
and is faster. However,thecompact
command is still slow and does block other database use. Only usecompact
during 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
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’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
compact
may 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.compact
requires a small amount of additional disk space while running but unlikerepairDatabase
it does not free space on the file system.You may also wish to run the
collStats
command before and after compaction to see how the storage space changes for the collection.compact
commands do not replicate to secondaries in a replica set:- Compact each member separately.
- Ideally, compaction runs on a secondary. See option
force:true
above for information regarding compacting the primary.
Warning
If you run
compact
on a secondary, the secondary will enter aRECOVERING
state to prevent clients from sending read operations during compaction. Once the operation finishes the secondary will automatically return toSECONDARY
state. Seestate
for more information replica set member states. Refer to the “partial script for automating step down and compaction” for an example of this procedure.compact
is a command issued to amongod
. In a sharded environment, runcompact
on 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.
compact
removes extra padding added to collections that have theusePowerOf2Sizes
flag set. See SERVER-4018 for more information.
See also
- force –