Consider a collection with age
field. The values of age
are from a known range of Integer
values.
When querying for users of age 20 to 23 years, the query can be written as:
"age": {
"$gte": 20
"$lte": 23,
},
Since we know the age range, the query can also be written as:
"age": {
"$in": [20,21,22,23]
},
Is the query performance better when using $in
or $gte
, $lte
?
db.collection.getIndexes()
? – kevinadi Feb 13 at 0:22age
index be any different whether$in
or$gt, $lt
is used? – Manuel Feb 13 at 1:27$in
is like a targeted query, while$gte
would be a range query. It's more complex once you introduce more fields, but have a look at emptysqua.re/blog/optimizing-mongodb-compound-indexes for a good explanation on how to optimize index use. – kevinadi Feb 13 at 4:02