开发者

How to make a query with or condition in mongoid

开发者_StackOverflow

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


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


This also works:

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


@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"])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜