Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

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.

Monday, October 14, 2013

Get the dot notation of a embedded Mongoid element

I needed to get the dot notation for embedded documents to perform queries using the given dot notation.

I could not find a "standard" way of doing just that, so here are some helper methods I use in a class. (Part of soon-to-come BoxCMS)

This Gist uses Mongoid metadata and reflect_on_all_associations, so it might break for newer versions of mongoid at some point.

Using dot notation queries in mongoid, makes it easy to dig for even the deepest "weird" embedded structures you might have.

Example of query:
»  Box::Page.where("rows.boxes.box_items.is_template_object" => false).first

Dot notations in mongodb are a powerful tool, but use it wisely unless you are indexing "more than you should".
Basic usage: Call dot_notation_for_element(embedded_element) to get the dot notation all the way up to the parent document, excluding the parent documents name/embedded name.