1

I have an an list of student which contain scores array of object, for example:

{
        "_id" : ObjectId("58a6c4542d0de393d2739979"),
        "student_id" : 0,
        "scores" : [
                {
                        "type" : "exam",
                        "score" : 76
                },
                {
                        "type" : "quiz",
                        "score" : 65
                },
                {
                        "type" : "homework",
                        "score" : 58
                }
        ],
        "class_id" : 438
}

I need to get student from collection, based on type and respective score with and condition for example

score greater than 60 in exam and

score greater than 50 in quiz and

score greater than 45 in homework

currently i'm trying the following query

db.students.find({$and: [{'scores.type': 'exam'}, {'scores.score': {$gt: 60}}]});

but its not working for single condition.

Any help greatly appreciated.

0

1 Answer 1

1

You can create query like this using $and and then create condition for each score type using $elemMatch and $gt

{
    $and: [
        {scores: {$elemMatch: {'type': 'exam','score': {$gt: 60}}}},
        {scores: {$elemMatch: {'type': 'quiz','score': {$gt: 50}}}},
        {scores: {$elemMatch: {'type': 'homework','score': {$gt: 45}}}}
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.