Why Mongoid?
The de facto ORM for MongoDB in Rails is Mongo Mapper, so why choose Mongoid? You can ready why straight from the horses mouth here.To summarize, Mongoid is built for Rails 3.0 and it handles larger documents better. It also feels like NoSQL when you use it. MongoMapper was built during Rails 2.x days and when MongoDB was young. It is modeled very closely to ActiveRecord to make the transition easier. Hence, it feels more like SQL. MongoMapper is more extensible though with a larger community.
MongoDB
Grab the latest build from MongoDB's download page. At this time, it is 1.6.3.Download and install. We make a softlink. This way, if we upgrade, we just switch the softlink and everything else stays the same.
cd /usr/local/src/ sudo wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.3.tgz sudo tar xzf mongodb-linux-i686-1.6.3.tgz sudo rm -rf mongodb-linux-i686-1.6.3.tgz sudo ln -s mongodb-linux-i686-1.6.3 mongodb
Add /usr/local/src/mongodb/bin to your path.
PATH=$PATH:/usr/local/src/mongodb/bin
Make /data/db and give ownership to your user. There are obviously several ways to do this, but this is the easiest.
sudo mkdir -p /data/db sudo chown -R wesley:wesley /data
Start your engines!
mongod
Mongoid
This builds on top of my previous post on installing Rails 3.0 with BDD. This assumes that the Rails application was created without ActiveRecord using -O. Check out Rails 3.0 agnosticism for an explanation.Add mongoid to Gemfile. bson_ext is installed for a speed boost.
gem 'mongoid', '2.0.0.rc.6' gem 'bson_ext', '~>1.2'
Install mongoid.
bundle install rails generate mongoid:config
Cucumber
Add a cucumber environment to mongoid.yml.cucumber: <<: *defaults database: myproject_cucumber
Cucumber makes use of Database Cleaner. There are posts saying Database Cleaner doesn't work with Mongoid. However, it seems like it is now according to the official documentation. We need to modify features/support/env.rb this is not recommended as it is regenerated on a cucumber-rails upgrade. Instead, we will create features/support/local_env.rb.
require 'database_cleaner' DatabaseCleaner.strategy = :truncation DatabaseCleaner.orm = "mongoid" Before { DatabaseCleaner.clean }
RSpec 2
RSpec includes ActiveRecord specific lines in spec/spec_helper.rb. You need to comment out the following 2 lines.# config.fixture_path = "#{::Rails.root}/spec/fixtures" # config.use_transactional_fixtures = true
To properly clean the database, RSpec needs to know how to do that with Mongoid. Again, we can use Database Cleaner for this.
Open up spec/spec_helper.rb and add the following in the RSpec.configure block
RSpec.configure do |config| # Other things # Clean up the database require 'database_cleaner' config.before(:suite) do DatabaseCleaner.strategy = :truncation DatabaseCleaner.orm = "mongoid" end config.before(:each) do DatabaseCleaner.clean end end
References
Updates
December 2, 2010: Changed Cucumber local_env.rb for database cleaning.December 2, 2010: Added a section for RSpec database cleaning.
February 1, 2011: Updated Mongoid version in the Gemfile
February 26, 2011: Added a note to clean out ActiveRecord specific lines in RSpec.