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!

Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • 1
    MongoMapper is still being actively developed. So, why the "need to migrate"? – WiredPrairie Dec 31 '13 at 01:06
  • 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. – Crashalot Dec 31 '13 at 01:24
  • While it may not have momentum, it's still being developed: https://github.com/mongomapper/mongomapper/commits/master. There's a comment about getting the new version on their github page: https://github.com/mongomapper/mongomapper – WiredPrairie Dec 31 '13 at 01:26
  • @Crashalot 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: https://www.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: https://www.versioneye.com/ruby/mongoid/3.1.6 MongoID is much more an active project, the dev speed is higher and the community larger. – Robert Reiz Jan 03 '14 at 17:28
  • 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? – Crashalot Jan 03 '14 at 18:41
  • I know this group here https://groups.google.com/forum/#!forum/mongoid. You have to join first, then you can post. Otherwise you can open a ticket here https://github.com/mongoid/mongoid/issues and ask for advice. – Robert Reiz Jan 04 '14 at 11:15
  • we cannot post on the group, @RobertReiz, even though we joined already. would you mind posting our question for us? – Crashalot Jan 06 '14 at 05:34
  • @Crashalot That's odd. Now I can't post either :-( – Robert Reiz Jan 09 '14 at 17:24

1 Answers1

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).

user2503775
  • 4,267
  • 1
  • 23
  • 41