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
  • 295
  • 5
  • 17

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,815
  • 7
  • 22
  • 33
Chaitanya
  • 202
  • 2
  • 10