I’m working with a MongoDB compound index like this:
db.users.createIndex({ gender: 1, city: 1, age: 1 })
Suppose city has only a few possible values, e.g., “Chicago”, “LA”, “NY”.
I can query either by leaving city out:
db.users.find({ gender: "M", age: { $gt: 25 } })
or by explicitly specifying all possible cities in $in:
db.users.find({ gender: "M", city: { $in: ["Chicago","LA","NY"] }, age: { $gt: 25 } })
From what I understand:
-
Leaving city out makes MongoDB internally use [MinKey, MaxKey] for the index scan.
-
Using $in with all possible values essentially covers the same range.
My questions:
-
If $in includes all possible values for a field, is there any performance difference compared to leaving the field out?
-
Does the number of possible values matter in this case, or will MongoDB treat both queries the same in terms of index scanning?
-
Are there any cases (e.g., very large datasets, range queries) where one approach might be preferable?
I want to understand if manually specifying all values in $in is ever beneficial when the field is fully covered.