I've built an aggregation that is returning this data:
[
{
"users": [
{
_id: 1,
name: "John",
age: 31
},
{
_id: 2,
name: "Jane",
age: 26
}
],
"teams": [
{
id: 1,
name: "Team 1",
color: "yellow"
},
{
id: 2,
name: "Team 2",
color: "red"
}
],
"moreTeams": [
{
id: 1,
name: "Team 1 - More",
},
{
id: 2,
name: "Team 2 - More",
},
{
id: 3,
name: "Team 3 - More",
extra: "extra"
}
]
}
]
How can I group "teams" and moreTeams into the same array (based on id), keeping all properties and eventually overriding where they have the same names?
Here's my desired result:
[
{
"users": [
{
_id: 1,
name: "John",
age: 31
},
{
_id: 2,
name: "Jane",
age: 26
}
],
"groupedTeams": [
{
id: 1,
name: "Team 1 - More",
color: "yellow"
},
{
id: 2,
name: "Team 2 - More",
color: "red"
},
{
id: 3,
name: "Team 3 - More",
extra: "extra"
}
]
}
]
I've tried using $unwind and $group with no success :(
Palyground example: https://mongoplayground.net/p/yZY8oQ9r-N1