0

I am trying to use the $UnionWith on my aggregation pipeline to do something like this:

ParentObject

db.getCollection("col").aggregate(
[
   //Filtering the master
   {$match:{'_id':ObjectId('62722c260ee18a304784d8cf')}},
   //then we want to bring all data which holds the same type
   {$unionWith: { coll: "col", pipeline: [{$match:{'Type':"$Type"}}]}}
])

Basically I want to return a list with the '62722c260ee18a304784d8cf' object and all other objects that holds the 'Type' property...

As far as I could see, it seems this would not be possible, or am I missing something?

Basically the "$Type" does not get resolved...

Thanks in advance!

PCBL

PCBL
  • 71
  • 1
  • 4
  • better provide example document from the collection and expected output , since it is not very clear what exactly you want to achieve with unionWith on same collection , maybe $lookup is your option ... – R2D2 Jun 15 '22 at 08:44
  • HI @R2D2, As simple as that: ``` _id Name Type 62722c260ee18a304784d8cf A D 62722c260ee18a304784d8cj B D 62722c260ee18a304784d8ck C D 62722c260ee18a304784d8ck D E ``` If I run my query from above, I expect to get A,B,C... D would not come because it does not have same type as A(62722c260ee18a304784d8cf) – PCBL Jun 15 '22 at 08:51
  • 1
    check this answer here can give you ideas: https://stackoverflow.com/questions/52674629/lookup-with-same-collection – R2D2 Jun 15 '22 at 08:51

0 Answers0