Using MongoDB with Rails

NoSQL databases have gone from being a curiosity to being the backend of enterprise web apps. Likewise, Rails has also matured enough to be adopted by large organizations while still being a startup favorite. Rails grew up with SQL so implementing a NoSQL database may seem to be unconventional. This guide will help any user be able to easily set up one of the most popular NoSQL databases, MongoDB.

When you set up your initial Rails project make sure to skip active record.

rails new my_app --skip-active-record

Active record uses migrations to control your database, which won’t be needed with MongoDB.

Use Mongoid. Mongoid is a ODM (object document mapper) framework for Mongo in Ruby. It will make the rest of the process much simpler and will help you manage your database without active record.

Mongoid is wrapped up in a gem which makes installation a breeze.

gem install mongoid

Although for me it worked better to just add the mongoid gem to the gem file.

You will also need to make sure that you run the config

rails generate mongoid:config

Here is an example of what to expect the config file to look like (minus a big chuck of the comments):

  # Configure available database clients. (required)
    # Defines the default client. (required)
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: my_app_development
      # Provides the hosts the default client can connect to. Must be an array
      # of host:port pairs. (required)
        - localhost:27017
        # Change the default write concern. (default = { w: 1 })
        # write:
        #   w: 1

        # Change the default read preference. Valid options for mode are: :secondary,
        # :secondary_preferred, :primary, :primary_preferred, :nearest
        # (default: primary)
        # read:
        #   mode: :secondary_preferred
        #   tag_sets:
        #     - use: web
        # (default: 5)
        # connect_timeout: 5

        # The timeout to wait to execute operations on a socket before raising an error.
        # (default: 5)
        # socket_timeout: 5

        # The name of the replica set to connect to. Servers provided as seeds that do
      database: my_app_test
        - localhost:27017
          mode: :primary
        max_pool_size: 1

MongoDB Data Modelling
The key point to note here is it not inheriting from ActiveRecord. Documents are the core objects in Mongoid and any object that is to be persisted to the database must include Mongoid::Document which can be seen below:

MongoDB based model

class Person
include Mongoid::Document

field :first_name, type: String
field :last_name, type: String

Mongoid by default stores documents in a collection in a pluralized form of the class name. For the above Person class, the collection the document would get stored in would be named people.

Some quick model configurations are

Defining a new field :

field :name, type: String
field :_id, type: String, default: ->{ name }

Localized fields

field :description, localize: true


index “” => 1
index “description.en” => 1


class User
include Mongoid::Document
field :name, type: String
embeds_many :posts

class Post
include Mongoid::Document
field :x, type: Integer
field :y, type: Integer
embedded_in :user

Once you have MongoDB setup, make sure to reference the docs for everything from how to query, to nested attributes. Blogs like Mongotips are also a great way to grow your knowledge of MongoDB while you are using it with Rails.


Now read this

Back 2 The Start…

These last few years have been a constant whirlwind. From leaving my job, to learning how to code, to becoming a freelancer, and moving beyond, I have been constantly learning. And while I have learned to sustain drinking from the fire... Continue →