I'm trying to get all the documents from a collection that have a variable amount of failed exams.
My collection is the following:
I have to retrieve all student that have for example 3 scores lower than 10.
The query I am currently running is the following:
Student.aggregate([
{
$project: {
_id: 0,
name: 1,
students: {
count: {
$size: {
$filter: {
input: "$results",
as: "result",
cond: {$lt: ["$$result.score", 10]}
}
}
}
}
}
}
])
How would I check if the count is $gte then for example 3?
CodePudding user response:
You are almost 100% done already! Just add
{$match: {"students.count": {$gte: 3}}
as a stage in the pipeline after $project
.