1

i have my documents like this after aggregation lookup

1.  lookup 


{
  from: 'dogs',
  localField: 'ton_id',
  foreignField: 'ton_id',
  as: 'catch'
}

the result after lookup :


_id:ObjectId("5deasn732bksd595bq65")

admin : Array
    0 : object
       _id:ObjectId("5dasdfkjh18364csd251as21")
       ton_id:"255"
       contact:"17942365"

catch: Array
    0 : object
       _id:ObjectId("5deasd64bcsd251as21")
       ton_id:"255"
       breed:"dog"
       age  :"5"

    1 : object  
       _id:ObjectId("5deasdsdfbwj83921as21")
       ton_id:"255"
       breed:"cat"
       age  :"7"

    2 : object  
       _id:ObjectId("5ddsgergdsdfbwj83921as21")
       ton_id:"255"
       breed:"cat"
       age  :"8"


Need to use $match and get only breed of cats like this and which has age 8

catch: Array


    0 : object  
       _id:ObjectId("5ddsgergdsdfbwj83921as21")
       breed:"cat"
       age  :"8"


tried this after using $lookup

$match

{catch: { $elemMatch: {'breed':"cat",'age':"8" } }}

it is returning no output

theUnknown
  • 263
  • 1
  • 3
  • 12
  • Does this answer your question? [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – Valijon Jan 19 '20 at 19:46
  • `$elemMatch` only works with `.find` method, not in aggregate command. Use [$filter](https://docs.mongodb.com/manual/reference/operator/aggregation/filter/index.html) instead of `$elemMatch` in aggregate pipeline – Valijon Jan 19 '20 at 19:50

0 Answers0