Filter records and push value into array

Hi, I have this schema:

{
  "id": "aa"
  "org_id": "xx",
  "user_id": "nn"
}

How can I filter my records by user_id and push all the org_id values in an array? Like this:

{
  "org_ids": ["xx", "yy", "aa"]
}

Thanks

Use $match to filter to the user_id records you want and then use $group stage to $addToSet to collect unique org_id values (presumably you’d be grouping by user_id?

Asya

1 Like