Note on upgrading to Mongoid 3

We’ve been using Mongoid 2.4 for one of our projects for quite some times and it has also gone production in a small group for like two month. It works well and I really love this ODM. Thanks to Durran.

Mongoid 3 has been out for like months. Although I have been wanting to try it out, especially when I looked at all the people discussing about it in the Email group, I didn’t find any time to do the upgrade in the last two months. Just yesterday I upgraded to Mongoid 3, and I think the following things are worth noting down.

  • If you are using devise for the user management, you need to clear all the cookies, since Mongoid now uses Moped::BSON::ObjectId instead of BSON::ObjectId.

  • Remove setting of Mongoid.add_language. It is removed since custom application exceptions in various languages has been removed.

  • Remove setting of config.mongoid.logger= in your application.rb and replace it with Mongoid.logger= in your Mongoid initializer or some place else equally, since the logger setting is moved to the top level.

  • Look for all places which uses store_in syntax, it has been changed to a more complex one which provides much more complete features, check it out here: Mongoid: Persistence

  • Remember to change all BSON::ObjectId to Moped::BSON::ObjectId

  • About sort: syntax of Criteria#order_by has changed a little bit, Posts.all.order_by(:name, :desc) which worked in 2.4 will throw you an exception of NoMethodError (undefined method '__sort_option__' for :accurate_created_at:Symbol): in Mongoid 3.0. But you could change to Posts.all.order_by({:name, :desc}) or Posts.all.desc(:name). I liked the latter one a lot.

  • Criteria#count(true) was a way to tell mongoid to count with respect to limit inside the queries in Mongoid 2. Now it’s removed since it would cause an additional call to DB, which is unnecessary as the result is already in your memory. You could use Post.all.limit(100).to_a.count.

That’s all I’ve done in my upgrade to Mongoid 3. It’s not all you might run into when you upgrade, so definitely remember to check out this one: Mongoid Upgrade

comments powered by Disqus