I noticed a 400% improvement -- ~800ms to ~200ms -- in overall render times by moving from MongoMapper to Mongoid.
The first major step -- after installing the Mongoid Gem -- was to configure the database connection. This basically uses the Ruby Mongo API connection method in a configure block, which makes things pretty easy and consistant with other Mongo implementations.
So for MongoMapper I had it's custom connection setup (easier and less code, true):
MongoMapper.database = "<database name>" if username && password MongoMapper.database.authenticate(username, password) end
For Mongoid, I use:
Mongoid.configure do |config| config.database = Mongo::Connection.new.db("<database name>") if username && password config.database.authenticate(username, password) end end
Second was to update the mapper classes (Modules for Rails developers).
So for MongoMapper I had:
class Post include MongoMapper::Document key :title, String key :body, String key :tag_ids, Array timestamps! many :tags, :in => :tag_ids end
class Tag include MongoMapper::Document key :name, String, :unique => true # go go ugly reverse mapping code def posts Post.where(:tag_ids => self.id).sort(:update.desc).to_a end def destroy Post.where(:tag_ids => self.id).each do |post| post.tag_ids.delete(self.id) post.save! end super end end
The refactor for Mongoid, looks like this:
class Post include Mongoid::Document include Mongoid::Timestamps field :title, :type => String field :body, :type => String has_and_belongs_to_many :tags end
class Tag include Mongoid::Document validates_uniqueness_of :name field :name, :type => String has_and_belongs_to_many :posts end
I did run in to some slight differences in querying and sorting.
Post.where(:title => "foobar")
I know, to similar to really matter, but good to know. Also, I believe the MongoMapper syntax actually works for Mongoid as well -- if you prefer it.
I figured I would take a second to discuss why I decided to migrate. I had working code using MongoMapper, so why go through the trouble of migrating. Well, a couple of reasons.
I'm done, enjoy!