Installation¶
On this page
Install the Gem¶
Mongoid is bundled as a gem, and is hosted on Rubygems. It can be installed manually or with bundler.
To install the gem manually:
To install the gem with bundler, include the following in your Gemfile:
Compatibility¶
Note the following compatibility matrix to determine if Mongoid is compatible with your Ruby version and MongoDB server. Note that Mongoid is currently compatible with Rails 5 and depends on ActiveModel 5.1 or higher, and thus requires Ruby 2.2.2 or higher.
Ruby Version | MongoDB 4.0.x | MongoDB 3.6.x | MongoDB 3.4.x | MongoDB 3.2.x | MongoDB 3.0.x | MongoDB 2.6.x |
---|---|---|---|---|---|---|
MRI 2.5.0+ | Yes | Yes | Yes | Yes | Yes | Yes |
MRI 2.4.1+ | Yes | Yes | Yes | Yes | Yes | Yes |
MRI 2.3.x | Yes | Yes | Yes | Yes | Yes | Yes |
MRI 2.2.2+ | Yes | Yes | Yes | Yes | Yes | YES |
MRI 2.1.x | No | No | No | No | No | No |
JRuby 9.2.x | Yes | Yes | Yes | Yes | Yes | Yes |
JRuby 9.1.x | Yes | Yes | Yes | Yes | Yes | Yes |
Refer to Rails considerations for the Ruby on Rails compatibility matrix.
Configuration¶
Mongoid configuration can be done through a mongoid.yml
that specifies your
options and clients. The simplest configuration is as follows, which sets the default
client to “localhost:27017” and provides a single database in that client named “mongoid”.
Rails Applications¶
You can generate a config file by executing the generator and then editing
myapp/config/mongoid.yml
to your heart’s desire. Mongoid will then handle
everything else from there.
When Mongoid loads its configuration, it chooses the environment to use based on the following order:
Rails.env
if using Rails.Sinatra::Base.environment
if using Sinatra.RACK_ENV
environment variable.MONGOID_ENV
environment variable.
If you are not using any rack based application and want to override the
environment programatically, you can pass a second paramter to load!
and
Mongoid will use that.
Depending on your version of Rails, you may also need to configure the orm to be Mongoid in application.rb.
Anatomy of a Mongoid Config¶
Let’s have a look at a full-blown mongoid.yml
and explain the full power
of what Mongoid can do. The following configuration has its explanation in the
comments above each key.
Logging¶
Changing logging options is done simply by telling Mongoid or the driver’s logger to have
a different level. Logging level is DEBUG
by default.