MongoDB compact command Detailed

Why compact

A picture is worth a thousand words

compact

Remove and drop the difference

To remove a MongoDB collection of documents in all, there are two ways

  • db.collection.remove({}, {multi: true})One by one document removed from the btree, and finally all documents are deleted, but the files will not be recovered physical space
  • db.collection.drop() A collection of physical files deleted, the space is immediately recovered

Overall, remove produces logic of free space that can be used to write new data immediately, but the total physical space occupied by the file will not be recovered immediately; usually only sustained write data, and physical space debris small, compact does not need to set some scenes, after remove the large amount of data, subsequent write may not be much, then if you want to reclaim the space, you need to explicitly call compact.

Impact on the reading and writing of compact command

a compact collection, the collection will be added where the DB

Guess you like

Origin yq.aliyun.com/articles/709526