Installation
Get the Mongoid gem from rubygems.org:
gem install mongoid -v "2.0.0.beta.15"
For performance improvements you will want to also install the mongo c extensions if
compatible with your system.
gem install bson_ext
For Rails applications add the gem dependency to your
Gemfile:
gem "mongoid", "2.0.0.beta.15"
gem "bson_ext", "1.0.4"
Setup of the database connection itself is handled by using automatic configuration with
a
config/mongoid.yml. You can generate a mongoid.yml via the following command:
rails generate mongoid:config
Executing this will also switch your default ORM to Mongoid
(i.e. rails g model will now generate Mongoid Documents
as your models):
An example configuration is as follows, with the default values showing:
config/mongoid.yml:
defaults: &defaults
host: localhost
slaves:
- host: slave1.local
port: 27018
- host: slave2.local
port: 27019
autocreate_indexes: false
allow_dynamic_fields: true
include_root_in_json: false
parameterize_keys: true
persist_in_safe_mode: false
raise_not_found_error: true
reconnect_time: 3
development:
<<: *defaults
database: control_development
test:
<<: *defaults
database: control_test
# set these environment variables on your prod server
production:
<<: *defaults
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
Configuration
Mongoid currently supports the following configuration options in
the mongoid.yml shown above:
allow_dynamic_fields: When attributes are not defined as fields but
added to an object, they will get fields added for them dynamically and will get
persisted. This defaults to true, and if set to false an error will get raised when
attempting to set a value that has no field defined.
autocreate_indexes: Defaults to false. When set to true Mongoid will attempt to
create indexes each time a class is loaded. This is not recommended for any environment
other than development or test.
include_root_in_json: Defaults to false. When set to true mongoid will
include the name of the root document and the name of each association as the
root element when calling #to_json on a model.
parameterize_keys: Tells Mongoid to convert basic special
characters in composite keys to SEO friendly substrings. Defaults to true.
persist_in_safe_mode: Tells Mongoid to perform all database
operations in MongoDB's safe mode. This will cause the driver to double
check operations and raise an error if they failed. Defaults to false,
switching to true will be safe but will be a performance hit.
raise_not_found_error: Will raise a Mongoid::Error::DocumentNotFound
when attempting to find a document by an id that doesnt exist.
Defaults to true, but when set to false will only return nil
for the same query.
reconnect_time: Tells Mongoid the max time to attempt reconnection
to the database if a connection failure has occured. Default is 3 seconds.
skip_version_check: If you are having issues authenticating against
MongoHQ or MongoMachine because of access to the system collection being
not allowed, set this to true.
Logging
You can define your own logger for Mongoid if you do not want to use the default
Rails logger in your Rails'
application.rb.
module MyApplication
class Application < Rails::Application
config.mongoid.logger = Logger.new($stdout, :warn)
end
end
Removing Active Record
Now that you have a mongoid.yml you can't wait to delete that pesky database.yml,
right? Do it and you'll start getting ActiveRecord errors all over the place.
You don't need ActiveRecord unless you're trying to use Mongo in concert
with a SQL database. Here's how you remove ActiveRecord from the most
recent version of Rails 3:
- Open yourapp/config/application.rb
-
Near the top, remove the line require 'rails/all' and add the following
three lines so you end up with this:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
- Start up your app and welcome yourself to a life without ActiveRecord!
Not Using Rails 3?
You can initialize Mongoid in it's configuration block, either by calling the above
mentioned methods directly or using
from_hash to load in a mongoid.yml:
Mongoid.configure do |config|
name = "control_development"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.slaves = [
Mongo::Connection.new(host, 27018, :slave_ok => true).db(name)
]
config.persist_in_safe_mode = false
end
OR
file_name = File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
@settings = YAML.load(ERB.new(File.new(file_name).read).result)
Mongoid.configure do |config|
config.from_hash(@settings[ENV['RACK_ENV']])
end