Explain how to clear the extra disk space occupied by mongodb

Time:2019-12-5

The first point is that mongodb will not release the occupied hard disk space, even drop collection, unless drop database. If a DB once had a large amount of data deleted after a period of time, hard disk space is 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 not large, it doesn’t take too long for dump, or it often backs up dump files, this method is very simple.

2. repair database
Running in the Mongo shell


db.repairDatabase()

Or


db.runCommand({ repairDatabase: 1 })

, the second method can take several other parameters


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

Repairdatabase is the only way in official documents to reclaim hard disk space.
repairDatabase is the appropriate and the only way to reclaim disk space.
When you have multiple shards and the amount of data is huge, the dump & restore method will take a lot of time. At this time, the advantage of the second method is obvious, that is, running repairdatabase on each shard separately, the result will be much faster.

PS: format the output of Mongo shell
If there is a large amount of data, the default output of Mongo shell is very messy and can hardly be read. We can use. Pretty () to solve this problem:


db.collection.find().pretty()

This kind of output will be much more beautiful, one line per field,

{
 "_id" : ObjectId("5396cd3823e97923ba689ef3"),
 "batch" : 66,
 "category" : 4,
 "cover_imgs" : [
 "/post_imgs/5396cd3823e97923ba689ef3/c_2.jpg",
 "/post_imgs/5396cd3823e97923ba689ef3/c_3.jpg",
 "/post_imgs/5396cd3823e97923ba689ef3/c_4.jpg"
 ],
 "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 TV series",
 "seq" : 27180,
 "serial" : false,
 "sort_index" : 0.997,
 "Source" site ":" Post Bar ",
 "updated_at" : ISODate("2014-06-18T09:04:55.228Z"),
 "visible" : true
}
{
 "_id" : ObjectId("5396c7ca23e97921fb7de8e4"),
 "batch" : 74,
 "category" : 4,
}

Configure it as default:
Add the following configuration to $home /. Mongorc.js and create it if it doesn’t exist.


DBQuery.prototype._prettyShell = true

In this way, you don’t need to use the pretty () method every time. Just go to DB. Collection. Find().

Recommended Today

Share a simple blog

Series articles: Share a useful tp6 background management system Add Google login authentication in thinkphp6 background Share a simple blog JrkBlog_ Tp6.0 easy blog – you deserve it Project introduction Jrkblog blog is based on yfcmf background development Improve the following functions: Phpquerylist data collection,Phpword exports a word document,Practical and easy-to-use API interface,Upload OSS, cos […]