Add Monogid to your Sinatra app with potentially zero-configuration. Lazily creates the database connection whenever needed.
Switch branches/tags
Nothing to show
Pull request Compare This branch is even with mattpuchlerz:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Add Monogid to your Sinatra app with potentially zero-configuration. Lazily create the database connection whenever needed.


It‘s available as a gem via, so use standard procedure:

$ gem install sinatra-mongoid-config

If you're using Bundler, just add gem "sinatra-mongoid-config" to your Gemfile.

Using the Extension

This extension works fine with both “classic” Sinatra apps, as well as “modular” apps which inherit from Sinatra::Base. How you use the extension varies slightly between styles.


require 'rubygems'
require 'sinatra'
require 'sinatra-mongoid-config'

get '/' do
  'It works!'


require 'rubygems'
require 'sinatra'
require 'sinatra-mongoid-config'

class MyApp < Sinatra::Base

  register Sinatra::MongoidConfig

  get '/' do
    'It works!'


Options & Defaults

All options are set using Sinatra’s standard set method. Remember that you can also change settings for each environment:

configure do
  set :mongo_db, 'the_database'
  set :mongo_port, 123

configure :production do
  set :mongo_db, 'the_other_database'
  set :mongo_user, 'the_user'
  set :mongo_password, 'the_password'


All configuration options have sensible defaults listed below, and depending on your situation, you may not have to set anything.

Option Default
:mongo_host ENV['MONGO_HOST'] || 'localhost'
:mongo_db ENV['MONGO_DB'] || self.app_to_db_name(app)
:mongo_port ENV['MONGO_PORT'] || Mongo::Connection::DEFAULT_PORT
:mongo_user ENV['MONGO_USER']
:mongo_password ENV['MONGO_PASSWORD']

Default DB Naming Convention

Did you notice that the default for :mongo_db calls self.app_to_db_name? That method attempts to be smart about what your database is named by using the class name of your app and the current environment. The real benefit here is for those who are creating "modular" apps, as they are named something other than the Sinatra default. Here are some examples:

App’s Class Name Environment Resulting Database Name
Refresh :development 'refresh_development'
RefreshChicago :test 'refresh_chicago_test'
RefreshChicago::Jobs :production 'refresh_chicago_jobs_production'
Sinatra::Application :development 'sinatra_application_development'