I have collection A with a non-unique index over "field1".
If I run:
db.A.explain().distinct("field1")
I get:
"winningPlan" : {
"stage" : "PROJECTION",
...
"inputStage" : {
"stage" : "DISTINCT_SCAN",
"keyPattern" : {
"field1" : 1.0
},
...
}
Which suggests it'll use the index for the distinct call.
However, in collection B with a non-unique index on "type2.key", if I run:
db.B.explain().distinct("type2.key")
I get:
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : []
},
...
}
which seems to mean it doesn't use the index.
Why can distinct use the index on collection A but not on collection B, and can I do something to force the use of the index?
Notes:
- collection
Bis a lot bigger then collectionA, is there a limit to the size of the indexdistinctcan use? - I've read: Count distinct values in mongoDB and MongoDB - distinct with query doesn't use indexes they don't help to explain the difference in behavior I'm seeing.
- Both collections are
sharded - mongodb version is 3.2.12
EXAMPLE DOCUMENT
{
"_id" : ObjectId("57d6c1cf691fa014e0615aa7"),
"type1" : [
{
"key" : "key1",
"field" : "value1",
},
{
"key" : "key2",
"field" : "value2",
}
],
"type2" : [
{
"key" : "key3",
"field" : "value3",
},
{
"key" : "key4",
"field" : "value4",
}
]
}
The index is on type2.key
db.B.explain("allPlansExecution").distinct("obj.field2")to see why it is not possible.filterspart looks suspicious. Do you have any query parameter there?When possibleis as vague as it gets... also, when trying the "allPlansExecution" mode, the explain never returns (it's a very big collection). Another thing I haven't mentioned is that this both collections are sharded, I'll add it to the question notes