Good day MongoDB Community,
I would like to ask for your help in creating the correct pipeline for my data:
{
"group": "A",
"subgroup": "A1",
"name": "Abby"
},
{
"group": "A",
"subgroup": "A2",
"name": "Andy"
},
{
"group": "A",
"subgroup": "A2",
"name": "Amber"
},
{
"group": "B",
"subgroup": "B1",
"name": "Bart"
}
I want to group by group first, then for each group, group by subgroup.
The names will also go to their respective subgroup and the count is showing the actual count.
My expected output is as follows:
{
"_id": "B",
"count": 1,
"subgroup": [
{
"_id": "B1",
"count": 1,
"names": ["Bart"]
}
]
},
{
"_id": "A",
"count": 3,
"subgroup": [
{
"_id": "A1",
"count": 1,
"names":[ "Abby"]
},
{
"_id": "A2",
"count": 2,
"names": ["Amber", "Andy"]
}
]
}
The aggregation and actual output can be seen in the playground: