Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce MongoDB storage space after deleting a large amount of data [duplicate]

I have a database in MongoDB, called dump. Currently, it reached 6GB in my server. I decided to delete 90% of data in this database to reduce the disk space it occupied. But after doing that its size is still 6GB, while the true storage size is only 250MB.

I guess this is the design of MongoDB? Is there any convinient way to reduce its size? Thank in advance.

like image 455
user342960 Avatar asked May 27 '11 11:05

user342960


People also ask

Why are MongoDB data files large in size?

fileSize is larger than storageSize because it includes index extents and yet-unused space in data files. While fileSize does decrease when you delete a database, fileSize does not decrease as you remove collections, documents or indexes.

When you remove a document from database in MongoDB remove it from disk?

FYI: MongoDB does not release disk space after you delete a document, instead, it will reuse that space for future documents, hence the storageSize being bigger than dataSize .

What is the fastest operation to clear an entire collection in MongoDB?

collection. remove() — MongoDB Manual.


2 Answers

Try (source):

$ mongo mydb
> db.repairDatabase()
like image 117
Denis de Bernardy Avatar answered Sep 23 '22 05:09

Denis de Bernardy


To compress the data files, you can run either start up MongoDB with mongod --repair, or connect to the database through the shell and run db.repairDatabase().

There's also a new compact command scheduled for v1.9 that will do in-place compaction.

like image 24
Chris Fulstow Avatar answered Sep 20 '22 05:09

Chris Fulstow