Back to All posts

MongoDB with Mongomapper and Ruby on Rails

Oct 2009

I’m sure we’ve all heard the pros & cons of the NoSQL movement so these will not be covered here. I’ve been experimenting with a number of alternatives to RDBMS for a while such as CouchDB, TokyoCabinet, Redis and recently MongoDB.

MongoDB (from “humongous”) is a scalable, high-performance, open source, schema-free, document-oriented database. Written in C++, MongoDB features:

“MongoDB bridges the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS (which provide structured schemas and powerful queries).”

– the MongoDB site.

The reason I’ve settled on MongoDB is (for me) it’s been the most useful – more so than straight KV stores such as Redis and Tokyo. Technically CouchDB is very similar to MongoDB, being a document store too, however MongoDB seems to ‘gel better’ (that’s a technical term).

To be able to use this MongoDB goodness in my Rails app, I’m making use of the MongoMapper gem as it’s the most popular one (github forks and watchers metric). There are other ORMs which you can use with MongoDB, Mongoid seems like a good alternative and the rest are documented here: http://www.mongodb.org/display/DOCS/Ruby+Language+Center.

Installation

Download and run mongoDB http://www.mongodb.org/display/DOCS/Downloads

Install the gems:

gem install mongo_mapper
gem install mongo_ext #the c extensions (for production)

Configure your environment, and remove AR:

# config/enviroment.rb

config.gem 'mongo'
config.gem 'mongo_mapper'

# remove AR
config.frameworks -= [ :active_record, :active_resource ]

Add a config file for your database:

# config/mongodb.yml
base: &base
 adapter: mongodb
 database: coolapp
#These are needed to authenticate with your db
#should it run on another server
 host: genesis.mongohq.com
 username: your-username
 password: your-password

development:
 <<: *base

test:
 <<: *base
 database: coolapp-test

production:
 <<: *base

And add an initializer to setup MongoMapper and friends:

# config/initializers/mongodb.rb

include MongoMapper

db_config = YAML::load(File.read(File.join(Rails.root, "/config/mongodb.yml")))

if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
 mongo = db_config[Rails.env]
 MongoMapper.connection = Mongo::Connection.new(mongo['host'] || 'localhost',
                          mongo['port'] || 27017,
                          :logger => Rails.logger)
 MongoMapper.database = mongo['database']
  if mongo['username'] && mongo['password']
   MongoMapper.database.authenticate(mongo['username'], mongo['password'])
 end
end

ActionController::Base.rescue_responses['MongoMapper::DocumentNotFound'] = :not_found

# Used for image uploads
# CarrierWave.configure do |config|
#  mongo = db_config[Rails.env]
#  config.grid_fs_database = mongo['database']
#  config.grid_fs_host = mongo['host'] || 'localhost'
#  config.grid_fs_access_url = "gridfs"
#  config.grid_fs_username = mongo['username']
#  config.grid_fs_password =mongo['password']
# end

# It's also possible to define indexes in the the model itself; however,
# a few issues are being worked out still. This is a temporary solution.
# Comment.ensure_index([["story_id", 1], ["path", 1], ["points", -1]])
# MongoMapper.ensure_indexes!

# Handle passenger forking.
# if defined?(PhusionPassenger)
#  PhusionPassenger.on_event(:starting_worker_process) do |forked|
#   MongoMapper.database.connect_to_master if forked
#  end
# end

Getting stuff done

Now I know what you might be thinking, ‘oh no not another ORM all my favourite gems won’t work!’, that is not the case! (*in some cases)

Usage

These Models are stripped down but you get the idea. Let’s say you’re using Devise, then your User model could look like this:

# models/user.rb
class User
 # Class Configuration :::::::::::::::::::::::::::::::::::::::::::::
 include MongoMapper::Document
 devise :authenticatable, :recoverable, :rememberable
 
# Attributes ::::::::::::::::::::::::::::::::::::::::::::::::::::::
key :email,String
key :username,String
key :comment_count, Integer
key :encrypted_password, String
key :password_salt, String
key :reset_password_token, String
key :remember_token, String
key :remember_created_at, Time
key :sign_in_count, Integer
key :current_sign_in_at, Time
key :current_sign_in_ip, String
timestamps!

 # Validations :::::::::::::::::::::::::::::::::::::::::::::::::::::
RegEmailName = '[\w\.%\+\-]+'
RegDomainHead= '(?:[A-Z0-9\-]+\.)+'
RegDomainTLD = '(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)'
RegEmailOk = /\A#{RegEmailName}@#{RegDomainHead}#{RegDomainTLD}\z/i

 validates_length_of :email, :within => 6..100, :allow_blank => true
validates_format_of :email, :with => RegEmailOk, :allow_blank => true
 
# Assocations :::::::::::::::::::::::::::::::::::::::::::::::::::::
many :comments
 
end

 

And if that user had comments the model could look like:

# models/comment.rb
class Comment
# Class Configuration :::::::::::::::::::::::::::::::::::::::::::::
include MongoMapper::Document

# Attributes ::::::::::::::::::::::::::::::::::::::::::::::::::::::
key :message, String
key :message_html, String 
key :user_id,ObjectId
key :username, String
timestamps!

# Validations :::::::::::::::::::::::::::::::::::::::::::::::::::::
validates_presence_of :message

# Assocations :::::::::::::::::::::::::::::::::::::::::::::::::::::
belongs_to :user

# Callbacks :::::::::::::::::::::::::::::::::::::::::::::::::::::::
before_create :set_username, :htmlify
after_create :increment_comment_count
 
private
def increment_comment_count
User.increment(user_id, :comment_count => 1)
end

def htmlify
self.message_html = RedCloth.new(message).to_html
end

def set_username
self.username = self.user.username
end
 
end

 

These are just to help kick-start your app development, for more complete examples see the open source apps and http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails :

Open Source Ruby Applications using MongoDB (and MongoMapper)

NewsMonger – A simple social news application demonstrating MongoDB and Rails http://github.com/banker/newsmonger/

Oupsnow – A bugtracker in Rails/MongoMapper

http://github.com/shingara/oupsnow/

Watchtower – An example app built with Sinatra, Mustache and MongoDB

http://github.com/kneath/watchtower/

Shapado – stackoverflow like question and answer site:

http://gitorious.org/shapado/shapado

mmmblog – a blogging engine by the same guys as Shapado (http://blog.ricodigo.com/)

http://gitorious.org/mmmblog

More at http://wiki.github.com/jnunemaker/mongomapper/projects-using-mongomapper

Pro Tips

  • MongoMapper uses a fork of the validatable gem – which has some differences with AR validations
  • Denormalization is needed to reduce round-trips to the DB
  • Change how you think of your data models – this is closer to an object store
  • The finders have nifty options, check out http://wiki.github.com/jnunemaker/mongomapper/whats-new (0.5.7)
  • Don’t be afraid to drop down and use to the Ruby MongoDB driver directly http://www.mongodb.org/display/DOCS/Ruby+Tutorial
  • Keep an eye out on the github commit feed as it’s a bit of a moving target still

Notable Sites

Summary

No migrations (yes!), storing arrays and hashes as attribute keys (sweet), and inplace updates. We’re not going to use MongoDB to build transactional systems any time soon, but for the majority of web applications it’s a perfect fit.