Advice on migrating from MongoMapper to Mongoid?
Asked Answered
A

1

9

It seems like Mongoid is now the superior ORM for Mongo based on performance and development activity. Unfortunately, we're on MongoMapper and need to migrate.

Are there any concerns or stumbling blocks we should be aware of? We have found a few outdated articles on Google and tried posting on the Mongoid Google Groups (though we were prohibited), but would love thoughts from SO members who have done this in the past.

We're on Rails 3.2.12.

Thanks!

Accretion answered 30/12, 2013 at 23:11 Comment(8)
MongoMapper is still being actively developed. So, why the "need to migrate"?Rask
Is MM still being actively developed? There hasn't been a push to Rubygems in months whereas Mongoid just pushed one in December. We would love to stay and avoid migration, but MM doesn't seem to have as much as momentum as Mongoid.Accretion
While it may not have momentum, it's still being developed: github.com/mongomapper/mongomapper/commits/master. There's a comment about getting the new version on their github page: github.com/mongomapper/mongomapperRask
@Accretion the newest version of mongo_mapper was released over 1 year ago, average release time is 31 days and 195 other GEMs are referencing it: versioneye.com/ruby/mongo_mapper/0.12.0 In comparison to that MongoID just released 1 month ago and has an average release time of 6 days and more then 600 GEMs are referencing it: versioneye.com/ruby/mongoid/3.1.6 MongoID is much more an active project, the dev speed is higher and the community larger.Sheared
thanks, @RobertReiz. we can't post on the google group for mongoid, any clue why? we're looking to get advice/tips on migrating from MM to mongoid. is there another active forum besides google groups?Accretion
I know this group here groups.google.com/forum/#!forum/mongoid. You have to join first, then you can post. Otherwise you can open a ticket here github.com/mongoid/mongoid/issues and ask for advice.Sheared
we cannot post on the group, @RobertReiz, even though we joined already. would you mind posting our question for us?Accretion
@Accretion That's odd. Now I can't post either :-(Sheared
C
20

Both of them are great MongoDB Libraries for Ruby. But if you want to switch, here are some notes:

Migrating MongoMapper ORM to Mongoid ORM - Notes

  • Configure the database connection.

  • Replace configuration yaml file(includes replica configuration).

  • Configure Mongoid specific options. e.g - raise_not_found_error: false. if you don't want an error every time a query returns nothing...

  • Change all models definations - include MongoMapper::Document to include Mongoid::Document

  • Change the format for all fields definitions.

  • In mongoid, you should specipy the timestamp: include Mongoid::Timestamps

  • Change validation. e.g: :in => ARRAY, will be: validates :name, presence: true, inclusion: { in: ARRAY }

  • Change indexes.

  • Change order_by format. e.g: MM: Model.all(:order => 'name'). Mongoid: Model.order_by('name ASC')

  • Error is a keyword in Mongoid. So if you have a model named Error, you should change it.

  • Pagination format is different, using another gem.

  • The primary key index entry in MM is id. In Mongoid it's _id, if you have other code relying on .id in the object JSON, you can override as_json function in your Model to create the JSON structure you want.

  • In MM, Model.fields(:id, :name) ,limits the fields returned from the database to those supplied to the method. In Mongoid it's Model.only(:name,:id)

  • Some queries changes:

    1. Selecting objects by array: MM: Model.where(:attr.in => [ ] ) and Model.where(:attr => [ ] ) . Mongoid is only: Model.where(:attr.in => [ ] )

    2. Map option of MM is equivalent to the Mid's pluck. Model.map(&:name) --to-- Model.pluck(:name)

    3. Mongoid doesn't support find query for nil. e.g: value = nil. Model.find(value) will throw an error : "Calling Document .find with nil is invalid". So in mongoid we should do: Model.find(value || "").

    4. In MM: Model.find_or_initialize_by_name("BOB"). In Mongoid Model.find_or_initialize_by(name: "BOB").

    5. MM can be used in those two options: Model.where({:name => 'BOB'}).first, and also Model.first({:name => 'BOB'}). Mongoid has only first option.

    6. In MM, to update multiple objects: Model.set({conditions},attr_to_update). In Mongoid: Model.where(conditions).update_all(attr_to_update).

Cormick answered 6/1, 2014 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.