I have the following collection:
{
_id: 12345,
quizzes: [
{
_id: 111111,
done: true
}
]
},
{
_id: 78910,
quizzes: [
{
_id: 22222,
done: false
}
]
}
I want to select the documents where a certain quiz from the quizzes was done
and want to make sure that it uses the appropriate index. So I use the following query:
Answer.find({ 'quizzes.0.done': true }).explain('queryPlanner');
Which returns:
queryPlanner: {
plannerVersion: 1,
namespace: 'iquiz.answers',
indexFilterSet: false,
parsedQuery: { 'quizzes.0.done': [Object] },
winningPlan: { stage: 'COLLSCAN', filter: [Object], direction: 'forward' },
rejectedPlans: []
}
The query is not using any index as seen from the output. I have tried the following indexes and none get used:
{ quizzes.done: 1 }
{ quizzes.[$**].done: 1 }
{ quizzes: 1 }
The only 1 that actually gets used:
{ quizzes.0.done: 1 }
However this is not really practical as I may target any quiz from the quizzes array not just the first one. Is there a certain syntax for the index in my case or this is a current limitation of mongodb? Thanks!
quizzes.$**
which works but makes the query significantly slower than without it. I would rather want a wildcard on the array index somethinglike{ quizzes.[$**].done: 1 }
as it is fast enough but this is wrong syntax i guess