Database Structure:
[
{
"_id": "12",
"title": "Vanella Icream",
"contain":"sugar",
"details": [
{
"flavour": "Vanella"
},
{
"weight": "6KG"
}
]
},
{
"_id": "3",
"title": "Pretzels",
"contain":"salt",
"details": [
{
"flavour": "Wheat"
},
{
"weight": "2KG"
}
]
}
]
Above database, contains an array details
"details": [
{
"flavour": "Vanella"
},
{
"weight": "6KG"
}
]
How to get the flavour
and weight
to use in $addToSet
and $group
in my Aggeration?
Like this:
'$group': {
'_id': 1,
'flavour': {
'$addToSet': '$flavour'
},
'weight': {
'$addToSet': '$weight'
}
}
after that my expected output
{
"_id": 0,
"flavour": [
"Vanella",
"Wheat",
],
"weight": [
"12KG",
"6KG",
]
}
How do I do this for an array? Anyone Knows ??