Options
- force:true Because this blocks all other activity (in v2.2, only for its database), the compact command returns an error when invoked on a replica set primary. Use force:true to run on a replica set primary.
- paddingFactor:<pf> (v2.2+). Specify a padding factor for the compacted documents. By default the documents are fully compacted with no padding. If you do updates on the documents which grow them, and especially if you have several indexes for the collection, you will want some padding. Default is 1.0 (no padding), maximum 4.0.
- paddingBytes:<bytes> (v2.2+.) Specify padding as an absolute number of bytes. Can be useful if your documents start at small but then grow a large amount; for example if they start 40 bytes long and then you grow then by 1KB, paddingBytes:1024 might be reasonable whereas paddingFactor:4.0 would only add 40*(4.0-1)=120 bytes of padding.
-
// example. always add at least 100 bytes of padding, and at least 10% of the doc size too > db.runCommand( { compact : 'mycollectionname', paddingBytes:100, paddingFactor:1.1 } )
At the beginning of compaction, indexes are dropped for the collection. At the end, indexes are rebuilt. Thus, if you kill the compaction in the middle of its operation, either with killOp or a server failure, indexes may be missing from your collection. If you are running with --journal, no data should ever be lost on a crash during compaction, although on startup the indexes will not be present. (Regardless of this, always do a backup before system maintenance such as this!) When the indexes are rebuilt, they will be in the 2.0 index format.
If there's a crash while the command is running, then as long as journaling is enabled, your data will be safe.
Additionally, if a compaction operation is interrupted, much of the existing free space in a collection may become un-reusable. In this scenario, it is recommended that compaction be run again, to completion, to restore use of this free space
主要说的是索引在执行中,会重建,如果意外终止,会丢失数据,不过如果开启了journal,数据相对来说安全很多,还有就是空闲空间会变成不能重用,需要再次执行compact,恢复空闲空间的使用。