Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing MongoDB database file size

Tags:

mongodb

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.

How big is a 16MB MongoDB document?

With its 16Mb limit, a MongoDB document can easily store around 2 million values of 64-bit numbers (also dates and booleans). But strings are a special case. Each UTF-8 character takes one byte.

Does MongoDB need lots of RAM?

MongoDB requires approximately 1 GB of RAM per 100.000 assets. If the system has to start swapping memory to disk, this will have a severely negative impact on performance and should be avoided.

How large should MongoDB documents be?

Document Size Limit The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.


UPDATE: with the compact command and WiredTiger it looks like the extra disk space will actually be released to the OS.


UPDATE: as of v1.9+ there is a compact command.

This command will perform a compaction "in-line". It will still need some extra space, but not as much.


MongoDB compresses the files by:

  • copying the files to a new location
  • looping through the documents and re-ordering / re-solving them
  • replacing the original files with the new files

You can do this "compression" by running mongod --repair or by connecting directly and running db.repairDatabase().

In either case you need the space somewhere to copy the files. Now I don't know why you don't have enough space to perform a compress, however, you do have some options if you have another computer with more space.

  1. Export the database to another computer with Mongo installed (using mongoexport) and then you can Import that same database (using mongoimport). This will result in a new database that is more compressed. Now you can stop the original mongod replace with the new database files and you're good to go.
  2. Stop the current mongod and copy the database files to a bigger computer and run the repair on that computer. You can then move the new database files back to the original computer.

There is not currently a good way to "compact in place" using Mongo. And Mongo can definitely suck up a lot of space.

The best strategy right now for compaction is to run a Master-Slave setup. You can then compact the Slave, let it catch up and switch them over. I know still a little hairy. Maybe the Mongo team will come up with better in place compaction, but I don't think it's high on their list. Drive space is currently assumed to be cheap (and it usually is).


It looks like Mongo v1.9+ has support for the compact in place!

> db.runCommand( { compact : 'mycollectionname' } )

See the docs here: http://docs.mongodb.org/manual/reference/command/compact/

"Unlike repairDatabase, the compact command does not require double disk space to do its work. It does require a small amount of additional space while working. Additionally, compact is faster."


I had the same problem, and solved by simply doing this at the command line:

mongodump -d databasename
echo 'db.dropDatabase()' | mongo databasename
mongorestore dump/databasename

Compact all collections in current database

db.getCollectionNames().forEach(function (collectionName) {
    print('Compacting: ' + collectionName);
    db.runCommand({ compact: collectionName });
});

If you need to run a full repair, use the repairpath option. Point it to a disk with more available space.

For example, on my Mac I've used:

mongod --config /usr/local/etc/mongod.conf --repair --repairpath /Volumes/X/mongo_repair

Update: Per MongoDB Core Server Ticket 4266, you may need to add --nojournal to avoid an error:

mongod --config /usr/local/etc/mongod.conf --repair --repairpath /Volumes/X/mongo_repair --nojournal

Starting with 2.8 version of Mongo, you can use compression. You will have 3 levels of compression with WiredTiger engine, mmap (which is default in 2.6 does not provide compression):

  • None
  • snappy (by default)
  • zlib

Here is an example of how much space will you be able to save for 16 GB of data:

enter image description here

data is taken from this article.