How to make a query with or condition in mongoid

偶尔善良 提交于 2019-12-12 15:58:01

问题


How can I make a query with or condition in Mongoid.


回答1:


Here is the solution for "OR" query in mongoid.

if you want query like below

select * from user where id = 10 or name = 'hitesh';

in rails with mongoid then you have to write query like this

User.any_of({id: 10},{name: 'hitesh'}).first




回答2:


This also works:

User.or({id: 10},{name: 'hitesh'})




回答3:


@Simone Carletti is right but you also can use "mongo-style" notation:

# Ruby 1.9+
Person.where(last_name: {"$in" => ["Penn", "Teller"]})
#Ruby 1.9-
Person.where(:las_name => {"$in" => ["Penn", "Teller"]})

or simply

Person.where(:last_name.in => ["Penn", "Teller"])

upd

Conditions for two fields?

Person.where(:last_name.in => ["Penn", "Teller"], :first_name.in => ["Pedro", "Julio"])


来源:https://stackoverflow.com/questions/7598450/how-to-make-a-query-with-or-condition-in-mongoid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!