Desired Outcome:
[
{
"_id": "nourishing",
"doc_count": 2,
"ingredients": {
"Broccoli": {
"5g": 1,
"10g": 1
},
"Tomatoes": {
"20g": 1,
"25g": 1
}
}
},
{
"_id": "unhealthy",
"doc_count": 3,
"ingredients": {
"soda": {
"200ml": 1,
"300ml": 2
},
"chips": {
"50g": 1,
"75g": 1,
"90g": 1
}
}
}
]
Aggregation Preview: Experiment1
db.collection.aggregate([
{
$group: {
"_id": "$category",
"ingredients": {
$push: "$$ROOT"
},
"doc_count": {
$sum: 1
}
}
}
])
Furthermore,
I've identified an Additional Step to transform the food items array as well: Experiment2
However, this process covers all data sets. I intend to apply the methodology in "Experiment2" solely for the ingredients category within each respective cluster entity.
This is akin to amalgamating both Experiment1 and Experiment2. How can I achieve this?