Skip to content
Adapter to use the MongoDB with Rail's ActiveRecord
Latest commit 0c49d4f Sep 14, 2009 Mike Dirolf implement establish_connection - allows passenger smart_spawning to w…
…ork w/o modifications
Failed to load latest commit information.
tasks initial checkin of plugin code Jan 22, 2009
README.rdoc MINOR: update readme Aug 11, 2009


ActiveRecord for Mongo

This plugin provides an ActiveRecord connection adapter for Mongo (

This plugin relies on the “mongo” Ruby Gem, which can be found at See the README file there for installation instructions.

After installing this plugin, you will need a db/schema.rb file and a database.yml file. See below for instructions.


As noted above, this plugin requires the “mongo” Ruby Gem, version 0.11 or higher.

To add this plugin to your Rails app, move (or link) this directory into your Rails app's vendor/plugins directory and name it mongo_record. In other words, this README.rdoc file should be



ActiveRecord requires a schema, but Mongo is a schema-free database. This means that you have to supply this plugin with a schema for your Mongo database that ActiveRecord can use.

This plugin reads the file named by ENV or, if that is not defined, db/schema.rb to get the database schema the application wants to use.

Sample schema.rb

In case you need to hand-generate a schema file (for example, you don't have a db/schema.rb file generated by “rake db:schema:dump”, here is a small sample.

ActiveRecord::Schema.define(:version => 0) do

  create_table "students", :force => true do |t|
    t.column "name", :string, :null => false

  create_table "addresses", :force => true do |t|
    t.column "student_id", :integer, :null => false
    t.column "street", :string
    t.column "city", :string
    t.column "state", :string
    t.column "postal_code", :string

  add_index "addresses", ["student_id"], :name => "fk_addresses_students"

  create_table "courses", :force => true do |t|
    t.column "name",     :string

  create_table "scores", :force => true do |t|
    t.column "student_id", :integer, :null => false
    t.column "course_id", :integer, :null => false
    t.column "grade", :float, :null => false


Sample database.yml

  adapter: mongo
  database: foo_development

  adapter: mongo
  database: foo_test

  adapter: mongo
  database: foo_production

Optional Logging

If you wish, you can use a Mongo capped collection for Rails log messages. To do so, edit the file init.rb and uncomment the code following the comment that says “Uncomment the following to log to Mongo using a capped collection.” See MongoRecord::LogDevice for details.

If enabled, log messages are sent to the capped collection named rails_log_{RAILS_ENV}.


Copyright 2009 10gen, Inc.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Something went wrong with that request. Please try again.