Thursday, April 3, 2014

Limit MongoDB disk usage on your machine

When doing local development, you might want to limit how much space mongodb uses.

Note: Do not do this in production unless you know what you are doing. It will affect performance.

If you are on Ubuntu perform these steps:

  1. Stop mongodb
    sudo service mongodb stop
  2. Edit the mongodb configuration
    sudo nano /etc/mongodb.conf
  3. Add this to the file and save/close:
    smallfiles=true
  4. Remove old journal files:
    sudo rm -rf /var/lib/mongodb/journal
  5. Start mongodb
    sudo nano service mongodb start

Dropping a database after running rspec tests with MongoMapper or Mongoid

The mongodb folder on my development computer was using 11 gb, and I needed to free up some space on /var, since that was on the / partition. (Yes, it is recommended to have it on separate partition, but that is another discussion).

In your rspec add this:

RSpec.configure do |config|
  config.after(:suite) do
    # for MongoMapper
    db = MongoMapper.database
    # for Mongoid
    # db = Mongoid.master
    # drop the database
    db.command({dropDatabase:1})
  end
end


For using mongo directly, you need to access the db object and trigger the command as above.