Explain how to clear the excess disk space occupied by MongoDB

  • 2020-05-30 21:15:03
  • OfStack

The first point is that mongodb will not free up the disk space it has taken up, even if it is drop collection, unless it is drop database. If an db once had a large amount of data deleted after 1 period of time, the hard disk space would be a problem. How to recover the extra space occupied by mongdodb? There are two ways
1. dump & restore

mongodump -d databasename -o /path/to/dump_dir
echo 'db.dropDatabase()' | mongo <databasename>
mongorestore -d <databasename> /path/to/dump_dir

If the amount of data is small, dump does not need much time, or if the dump files are often backed up, this method is simple.

2. repair database
That is, it runs in mongo shell



db.runCommand({ repairDatabase: 1 })

, the second method can take several other parameters

{ repairDatabase: 1,
 preserveClonedFilesOnFailure: <boolean>,
 backupOriginalFiles: <boolean> }

repairDatabase is the only way the official documentation says it can reclaim disk space.
repairDatabase is the appropriate and the only way to reclaim disk space.
When you have multiple shard and a huge amount of data, dump & The restore method takes a lot of time, and the advantage of the second method is that running repairDatabase on each shard is much faster.

PS: formats the output of mongo shell
If you have a large amount of data, mongo shell's default output is messy and almost unreadable. We can use.pretty () to solve a problem like this:


This is a much more beautiful output. For each row of field1,

 "_id" : ObjectId("5396cd3823e97923ba689ef3"),
 "batch" : 66,
 "category" : 4,
 "cover_imgs" : [
 "created_at" : ISODate("2014-06-10T09:18:06.383Z"),
 "fav_count" : 0,
 "host_reply_count" : 338,
 "last_reply_date" : "2014-06-17 21:22:00",
 "post_date" : "2014-06-06 19:57:00",
 "referer" : "http://tieba.baidu.com/f?kw=%B9%C5%D7%B0%B5%E7%CA%D3%BE%E7",
 "reply_count" : 716,
 "reuse_type" : 2,
 "section" : " Costume drama ",
 "seq" : 27180,
 "serial" : false,
 "sort_index" : 0.997,
 "source_site" : " tieba ",
 "updated_at" : ISODate("2014-06-18T09:04:55.228Z"),
 "visible" : true
 "_id" : ObjectId("5396c7ca23e97921fb7de8e4"),
 "batch" : 74,
 "category" : 4,

Configure it to be Default:
Add the following configuration to $HOME/.mongorc.js or create it if it does not exist.

DBQuery.prototype._prettyShell = true

Instead of using the pretty() method every time, just db.collection.find ().

Related articles: