0

I'm trying to get all the documents from a collection that have a variable amount of failed exams.

My collection is the following: Student db in mongoDB

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?

My current output: Query output

1 Answer 1

1

You are almost 100% done already! Just add

{$match: {"students.count": {$gte: 3}}

as a stage in the pipeline after $project.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.