Reputation: 5174
If I have a few IDs, say [1,2,3]
Is it possible to query them all at once in Mongoid? Such as:
User.where({ id: [1,2,3]})
or something similar?
Upvotes: 3
Views: 9010
Reputation: 328
Bit late to the party, but you can just pass an array of IDs to the find() method.
User.find(ids)
Upvotes: 6
Reputation: 434685
The underlying MongoDB query you're looking for would use the $in
operator:
The $in operator selects the documents where the value of a field equals any value in the specified array.
In MongoDB, you'd say:
db.users.find({ id: { $in: [1,2,3] } })
That translates directly into Mongoid as:
User.where(id: { :$in => [1,2,3] })
Mongoid patches most (all?) of the query operators into Symbol
so you'd usually say:
User.where(:id.in => [1,2,3])
Upvotes: 12