Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the way to clean up the excess disk space occupied by MongoDB

Shulou Source: shulou.com Published: 2022-05-31 15:44:12 09月19日 Update

In this issue, the editor will bring you about the method of cleaning up the excess disk space occupied by MongoDB. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The first point is that mongodb does not release the hard disk space that is already occupied, not even drop collection, unless drop database. If a db once had a large amount of data and then deleted after a period of time, hard disk space is a problem, how to recover the excess space occupied by mongdodb? There are two ways:

1. Dump & restore

Mongodump-d databasename-o / path/to/dump_dir

Echo 'db.dropDatabase ()' | mongo

Mongorestore-d / path/to/dump_dir

If the amount of data is small, dump does not take too long, or there are frequent backups of dump files, this method is simple.

2. Repair database

That is, run db.repairDatabase () in mongo shell, or

Db.runCommand ({repairDatabase: 1})

The second method can take several other parameters

{repairDatabase: 1

PreserveClonedFilesOnFailure:

BackupOriginalFiles:}

RepairDatabase is the only way to reclaim hard disk space in official documentation.

RepairDatabase is the appropriate and the only way to reclaim disk space.

When you have multiple shard and a large amount of data, the dump & 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: formatting the output of mongo shell

If there is a large amount of data, the default output of mongo shell is so messy that it is almost impossible to read. We can solve this problem with .resume ():

Db.collection.find () .pretty ()

The output would 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 to be Default:

Add the following configuration to $HOME/.mongorc.js, or create it if it doesn't exist.

DBQuery.prototype._prettyShell = true

This eliminates the need to use the pretty () method every time, but simply db.collection.find ().

The above is the method shared by Xiaobian to remove the excess disk space occupied by MongoDB. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Method space that is data situation hard disk output disk huge content time problem analysis run configuration no obvious beautiful one line not big Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Xiaomi MariaDB vpn