Awesome Open Source
Awesome Open Source


Build Status Gem Version Code Climate


MongoSessionStore is a Rails-compatible session store for MongoDB using either Mongoid or the MongoDB Ruby Driver. It also allows for custom Mongo session store that works with any (or no!) Mongo ODM.

MongoSessionStore version 3 is compatible with Rails 4.0 through 5.2. For Rails 3 support please check out issue #17 for options and let us know if you need support.


Add the mongo_session_store gem to your Gemfile. Use either the mongo or mongoid gems in combination with this gem.

# Gemfile

gem "mongoid"
# or gem "mongo"
gem "mongo_session_store"

Configure the session store in the session_store initializer in your Rails project.

# config/initializers/session_store.rb

# Mongoid
MyApp::Application.config.session_store :mongoid_store

# MongoDB Ruby Driver/anything else
MongoStore::Session.database =[""], database: "my_app_development")
MyApp::Application.config.session_store :mongo_store


By default, the sessions will be stored in the "sessions" collection in MongoDB. If you want to use a different collection, you can set that in the session_store initializer.

# config/initializers/session_store.rb
MongoSessionStore.collection_name = "client_sessions"


By default nothing has to be done outside the installation of the gem.

It's possible to query your sessions.

# Mongoid
MongoidStore::Session.where( => 2.days.ago)

# MongoDB Ruby Driver
MongoStore::Session.where("updated_at" => { "$gt" => 2.days.ago })



To run the tests for a specific store. You must first set a BUNDLE_GEMFILE in the environment.

bundle exec rake


BUNDLE_GEMFILE=gemfiles/rails-4.0-mongo.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-4.0-mongoid.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-4.1-mongo.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-4.1-mongoid.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-4.2-mongo.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-4.2-mongoid.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-5.0-mongo.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-5.0-mongoid.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-5.1-mongo.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-5.1-mongoid.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-5.2-mongo.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-5.2-mongoid.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-6.0-mongo.gemfile bundle exec rake
BUNDLE_GEMFILE=gemfiles/rails-6.0-mongoid.gemfile bundle exec rake

Performance benchmark

The repository includes a performance benchmark. It runs against all available included stores and outputs the results.

bundle exec ruby perf/benchmark.rb


To create a new release checkout the master branch and make sure it's in the right state to release. Run the release Rake task and follow the instructions.

bundle exec rake release


MongoSessionStore started as a fork of the DataMapper session store, modified to work with MongoMapper, Mongoid and the Mongo Ruby Driver.

Much thanks to all contributors:

  • Nicolas Mérouze
  • Chris Brickley
  • Tony Pitale
  • Nicola Racco
  • Matt Powell
  • Ryan Fitzgerald
  • Brian Hempel
  • Tom de Bruijn


Released under the MIT license. See the LICENSE file.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Ruby (312,071
Mongodb (14,143
Ruby On Rails (6,369
Session (585
Session Management (166
Related Projects