|
$matching on a positional path only seems to work as expected within a $facet if there are no subsequent stages. $count, $project, $group all behave as though they receive zero input documents, even though results are correctly returned when no subsequent stage is provided.
$sort, however, seems to function correctly.
original description
The below script leads to an empty scoreRank:
db.test.drop();
|
|
db.test.insertMany([{
|
"_id": 12345,
|
"quizzes": [
|
{ "score": 100 }
|
]
|
},
|
{
|
"_id": 789,
|
"quizzes": [
|
{ "score": 200 }
|
]
|
}
|
]);
|
|
const res = db.test.aggregate([
|
{
|
$facet: {
|
scoreRank: [
|
{ $match: { 'quizzes.0.score': { $gt: 0 } } },
|
{ $count: 'count' }
|
]
|
}
|
}
|
]).toArray();
|
|
print(JSON.stringify(res, null, ' '));
|
I get the below output:
If you replace 'quizzes.0.score' with 'quizzes.score', move $match out of $facet, or move the entire scoreRank pipeline out of $facet I get the expected result:
[
|
{
|
"scoreRank": [
|
{
|
"count": 2
|
}
|
]
|
}
|
]
|
|