Reputation: 41
My document structure is something like this
{
_id: "id1",
field1: "val1",
field2: "val2",
outcome: "ABC"
}
I have created index on outcome field. I have to find all documents with {outcome:"ABC"}
or {outcome:"XYZ"}
only. There is no major difference in query execution time if i use $or
or $in
. e.g
db.coll.find({$or:[{outcome:"ABC"},{outcome:"XYZ"}]});
db.coll.find({outcome:{$in:["ABC","XYZ"]}});
Which operator should i use $or
or $in
in this case? And why? Any help would be appreciated.
Upvotes: 2
Views: 59
Reputation: 48506
MongoDB docs
When using
$or
with<expressions>
that are equality checks for the value of the same field, use the$in
operator instead of the$or
operator.For example, to select all documents in the inventory collection where the quantity field value equals either
20
or50
, use the$in
operator:db.inventory.find ( { quantity: { $in: [20, 50] } } )
So in your case it is better to use
db.coll.find({outcome:{$in:["ABC","XYZ"]}});
Upvotes: 2