How to make a query with or condition in mongoid
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"])
精彩评论