Getting Started (Sinatra)

New Application

This section shows how to create a new Sinatra application using Mongoid for data access. The process is similar for other Ruby applications not using Ruby on Rails.

The complete source code for this application is available in the mongoid-demo GitHub repository.

Create Git Repo

While not required, we recommend creating a Git repository for your application:

git init blog
cd blog

Commit your changes as you are following this tutorial.

Create Gemfile

Create a file named Gemfile with the following contents:

source 'https://rubygems.org'

gem 'sinatra'
gem 'mongoid'
gem 'puma'

Install Dependencies

Run the following commands to install the dependencies:

gem install bundler
bundle install

This command will generate a file named Gemfile.lock which we recommend committing to your Git repository.

Run MongoDB Locally

To develop locally with MongoDB, download and install MongoDB.

Once MongoDB is installed and running, create a file named config/mongoid.yml pointing to your deployment. For example, if you launched a standalone mongod on the default port, the following contents would be appropriate:

      database: blog_development
        - localhost:27017
        server_selection_timeout: 1

Use MongoDB Atlas

Instead of downloading, installing and running MongoDB locally, you can create a free MongoDB Atlas account and create a free MongoDB cluster in Atlas. Once the cluster is created, follow the instructions in connect to the cluster page to obtain the URI. Use the Ruby driver 2.5 or later format.

Create a file named config/mongoid.yml with the following contents, replacing the URI with the actual URI for your cluster:

      uri: mongodb+srv://user:pass@yourcluster.mongodb.net/blog_development?retryWrites=true&w=majority
        server_selection_timeout: 5

Basic Application

Create a file named app.rb with the following contents. First, some requires:

require 'sinatra'
require 'mongoid'

Load the Mongoid configuration file and configure Mongoid. This is done automatically when Mongoid is used with Rails, but since we are using Mongoid with Sinatra, we need to do this ourselves:

Mongoid.load!(File.join(File.dirname(__FILE__), 'config', 'mongoid.yml'))

Now we can define some models:

class Post
  include Mongoid::Document

  field :title, type: String
  field :body, type: String

  has_many :comments

class Comment
  include Mongoid::Document

  field :name, type: String
  field :message, type: String

  belongs_to :post

... and add some routes:

get '/posts' do

post '/posts' do
  post = Post.create!(params[:post])

get '/posts/:post_id' do |post_id|
  post = Post.find(post_id)
    comments: post.comments,

post '/posts/:post_id/comments' do |post_id|
  post = Post.find(post_id)
  comment = post.comments.create!(params[:comment])

Run Application

Launch the application:

bundle exec ruby app.rb

Try some requests via curl:

curl http://localhost:4567/posts
# => []

curl -d 'post[title]=hello&post[body]=hello+world' http://localhost:4567/posts
# => {"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"body":"hello world","title":"hello"}

curl http://localhost:4567/posts
# => [{"_id":{"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"body":"hello world","title":"hello"}]

curl -d 'comment[name]=David&comment[message]=I+like' http://localhost:4567/posts/5d8151ec96fb4f0ed5a7a03f/comments
# => {}

curl http://localhost:4567/posts/5d8151ec96fb4f0ed5a7a03f
# => {"_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"},"title":"hello","body":"hello world","comments":[{"_id":{"$oid":"5d8157ac96fb4f20c5e45c4d"},"message":"I like","name":"David","post_id":{"$oid":"5d8151ec96fb4f0ed5a7a03f"}}]}

Existing Application

To start using Mongoid in an existing Sinatra applications, the steps are essentially the same as the one given above for a new application:

  1. Add the mongoid dependency to the Gemfile.
  2. Create a mongoid.yml configuration file.
  3. Load the configuration file and configure Mongoid in the application.
  4. Define Mongoid models.