0

A document in my collection looks like below:

{
   "_id":"<uuid>",
   "tests":[
      {
         "min":5,
         "max":20
      },
      {
         "min":15,
         "max":35
      }
   ]
}

Now, I want to find all the tests in which min >= 5.

db.coll.aggregate([
  { $match: { 'tests.min': {$gte : 5}} }, 
  { '$unwind': "$tests" }
]);

the above query somehow doesnt return the correct results.

Chandan Bhattad
  • 351
  • 1
  • 5
  • 21

1 Answers1

1

You do not need to write a aggregate query for this. Use the $elemMatch operator for this. Your query will look like as shown

db.coll.find({
    tests: {
        $elemMatch: {
            min: {
                $gte: 5
            }
        }
    }
})

The above query will return the desired result. For more details visit $elemMatch (query)

Adeel
  • 2,901
  • 7
  • 24
  • 34
Chaitanya
  • 237
  • 2
  • 11