|
v2.0+ The compact command compacts and defragments a collection. Indexes are rebuilt at the same time. It is conceptually similar to repairDatabase, but works on a single collection rather than an entire database. To run (example from the mongo shell): > db.runCommand( { compact : 'mycollectionname' } )
OR
> db.mycollection.runCommand( "compact" )
The command will not return until compaction completes. You can view intermediate progress in either the mongod log file, or by running db.currentOp() in another shell instance. Because this blocks all other activity, the compact command returns an error when invoked on a replica set primary. To force it to run on a replica set primary, include force:true in the command as a parameter. Running a compactionThe compact command generally uses less disk space while performing its work than repairDatabase does (though in worst case can need just as much). Additionally, compact is faster. Although faster than repairDatabase, this command blocks all other operations while running, and is still slow. Run during scheduled maintenance. If you run the command on a replica set secondary, the secondary will automatically demote itself to a "recovery" state until the compaction is complete. Killing a compactionAt 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. Effects of a compaction
You may wish to run the collstats command (db.collectionname.stats() in the shell) before and after compaction to see how the storage space changes for the collection. Compaction may increase the total size and number of your data files, especially when running compact for the first time on a collection. Even in this case, total collection storage space consumed will not increase. File SystemThis operation will not reduce the amount of disk space used on the filesystem. Storage size is the amount of data allocated within the database files, not the size/number of the files on file system. Details
Replica Sets
See Also |

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.
blog comments powered by Disqus