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:


Download and run mongoDB

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
 username: your-username
 password: your-password

 <<: *base

 <<: *base
 database: coolapp-test

 <<: *base

And add an initializer to setup MongoMapper and friends:

# config/initializers/mongodb.rb

include MongoMapper

db_config = YAML::load(, "/config/mongodb.yml")))

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

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)


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

 # 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


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

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

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

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

def htmlify
self.message_html =

def set_username
self.username = self.user.username


These are just to help kick-start your app development, for more complete examples see the open source apps and :

Open Source Ruby Applications using MongoDB (and MongoMapper)

NewsMonger – A simple social news application demonstrating MongoDB and Rails

Oupsnow – A bugtracker in Rails/MongoMapper

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

Shapado – stackoverflow like question and answer site:

mmmblog – a blogging engine by the same guys as Shapado (

More at

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 (0.5.7)
  • Don’t be afraid to drop down and use to the Ruby MongoDB driver directly
  • Keep an eye out on the github commit feed as it’s a bit of a moving target still

Notable Sites


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.

Stay up to date with our blog posts

Keep up-to-date with our latest blog posts, news, case studies, and thought leadership.

  • This field is for validation purposes and should be left unchanged.

We’ll never share your information with anyone else. Read our Privacy Policy.