Home > Mobile >  MongoDB (Mongoose) database query question
MongoDB (Mongoose) database query question

Time:12-12

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

CodePudding user response:

You are almost 100% done already! Just add

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

as a stage in the pipeline after $project.

  • Related