Explain the way to clear the extra disk space occupied by MongoDB _mongodb

Source: Internet
Author: User
Tags mongodb mongo shell

The first point is that MongoDB will not release the hard disk space already occupied, even if drop collection is not possible, unless drop database. If a db once had a large amount of data after a period of time and then deleted, hard disk space is a problem, how to recover the Mongdodb occupied by the extra space? There are two kinds of methods
1. Dump & Restore

mongodump-d databasename-o/path/to/dump_dir
Echo ' db.dropdatabase () ' | MONGO <databasename>
Mongorestore-d <databasename>/path/to/dump_dir

This approach is simple if the amount of data is small and the dump does not take too long, or when a dump file is frequently backed up.

2. Repair Database
that runs 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 method in the official document that can reclaim hard disk space.
The repairdatabase is the appropriate and the way to reclaim disk spaces.
When you have multiple shard and large amounts of data, the Dump & Restore method takes a huge amount of time, and the advantage of the second approach is that it's very obvious that running repairdatabase on each shard will result in a lot faster.

PS: Format the output of the MONGO shell
if the volume of data is large, the default output of the MONGO shell is messy and almost impossible to read. We can use. Pretty () to solve the problem:

Db.collection.find (). Pretty ()

Such output would be much prettier, each field line,

{
 "_id": ObjectId ("5396CD3823E97923BA689EF3"),
 "batch": $,
 "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 drama ",
 " seq ": 27180,
 " serial ": false,
 " Sort_ Index ": 0.997,
 " source_site ":" Stick ",
 " Updated_at ": Isodate (" 2014-06-18t09:04:55.228z "),
 " visible ": True
}
{
 "_id": ObjectId ("5396c7ca23e97921fb7de8e4"),
 "batch":,
 "category": 4,
}

Configure to make it default:
Add the following configuration to $home/.mongorc.js and create if it does not exist.

Dbquery.prototype._prettyshell = True

This does not require each use of the pretty () method, direct Db.collection.find () can be.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.