从MongoMapper迁移到Mongoid的建议? [英] Advice on migrating from MongoMapper to Mongoid?

查看:106
本文介绍了从MongoMapper迁移到Mongoid的建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于性能和开发活动,看来Mongoid现在是Mongo的高级ORM.不幸的是,我们在MongoMapper上,需要迁移.

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.

我们应该注意任何问题或绊脚石吗?我们已经在Google上找到了一些过时的文章,并尝试在Mongoid Google网上论坛上发帖(尽管我们被禁止了),但是很喜欢过去这样做的SO成员的想法.

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.

我们正在使用Rails 3.2.12.

We're on Rails 3.2.12.

谢谢!

推荐答案

两者都是出色的Ruby MongoDB库.但是,如果要切换,请注意以下几点:

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

将MongoMapper ORM迁移到Mongoid ORM-注释

  • 配置数据库连接.

  • Configure the database connection.

替换配置yaml文件(包括副本配置).

Replace configuration yaml file(includes replica configuration).

配置Mongoid特定选项.例如-raise_not_found_error: false.如果您每次查询都不返回错误时不想出错...

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

更改所有模型定义-从include MongoMapper::Document更改为include Mongoid::Document

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

更改所有字段定义的格式.

Change the format for all fields definitions.

在蒙古语中,应指定时间戳:include Mongoid::Timestamps

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

更改验证.例如::in => ARRAY,将是:validates :name, presence: true, inclusion: { in: ARRAY }

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

更改索引.

更改订单格式.例如:MM:Model.all(:order => 'name'). Mongoid:Model.order_by('name ASC')

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

Error是Mongoid中的关键字.因此,如果您有一个名为Error的模型,则应该对其进行更改.

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

使用另一个gem的分页格式是不同的.

Pagination format is different, using another gem.

MM中的主键索引条目为id.在Mongoid中,它是_id,如果您有其他代码依赖对象JSON中的.id,则可以覆盖

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.

在MM中,Model.fields(:id, :name)将从数据库返回的字段限制为提供给方法的字段.在Mongoid中,它是Model.only(:name,:id)

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)

某些查询发生了变化:

  1. 通过数组选择对象:MM:Model.where(:attr.in => [ ] )Model.where(:attr => [ ] ).仅限Mongoid:Model.where(:attr.in => [ ] )

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

Map选项等效于Mid的弹拨. Model.map(&:name) --to-- Model.pluck(:name)

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

Mongoid不支持nil的查找查询.例如:value = nil. Model.find(value)将引发错误:"Calling Document .find with nil is invalid".因此,在蒙古包中,我们应该做:Model.find(value || "").

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

在MM中:Model.find_or_initialize_by_name("BOB").在Mongoid Model.find_or_initialize_by(name: "BOB").

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

MM可以在这两个选项中使用:Model.where({:name => 'BOB'}).firstModel.first({:name => 'BOB'}).蒙古人只有第一选择.

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

在MM中,要更新多个对象:Model.set({conditions},attr_to_update).在Mongoid中:Model.where(conditions).update_all(attr_to_update).

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

这篇关于从MongoMapper迁移到Mongoid的建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆