问题
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