$filter and $addToSet for Array in Mongodb?

Structure:

[
 {
  "_id": "12",
  "title": "Vanella Icream",
  "contain":"sugar",
  "details": [
      {
        "flavour": "Vanella"
      },
      {
        "weight": "6KG"
      }
   ]
 },
 {
  "_id": "3",
  "title": "Pretzels",
  "contain":"salt",
  "details": [
      {
        "flavour": "Wheat"
      },
      {
        "weight": "2KG"
      }
   ]
 }
]

Expected Output

details:
{
      "flavour": [
        "Vanella",
        "Wheat",
      ],
      "weight": [
        "6KG",
        "2KG",
      ]
}

I’ve tried like this with $addToSet, $filter $group but I’m not getting result like that. any one please suggest. Mongo Playground: Mongo playground

See this there are answers for this and for the updated one you asked.

I think something went wrong, and the one i was answering was deleted, but i found the same question duplicated by mistake, so i posted there the answer, hope all ok with your acount.

Both answers are complicated and slow, because you wanted in the answer to have data in keys, keys are better for the schema, and values to go to arrays or values in objects , not keys.

Hi Takis. Your answer reyally helped me. yes question deleted. but I want result without using flavour, weight or sugar anywhere in my aggeration. Question here: javascript - Count of Items in array - MongoDB - Stack Overflow

You have used flavour and weight in $filter stage. i don’t want that. hope you understand. Your playground: Mongo playground

I’ve also added sugar field in that but its not showing in output because you have added weight and flavour separately in $filter. Please show me another way of doing this @Takis