Database

[{"_id":"1","title":"Vanella Icream","contain":"sugar","details":[{"flavour":"Vanella"},{"weight":"10KG"},{"sugar":"15KG"}]},{"_id":"2","title":"Pretzels","contain":"salt","details":[{"flavour":"Wheat"},{"weight":"10KG"},{"sugar":"15KG"}]},{"_id":"3","title":"Rasmalai Icream","contain":"sugar","details":[{"flavour":"Vanella"},{"weight":"15KG"},{"sugar":"12KG"}]},{"_id":"4","title":"Vanella Icream","contain":"sugar","details":[{"flavour":"Vanella"},{"weight":"15KG"},{"sugar":"12KG"}]}]

Query

db.collection.aggregate([{"$unwind":{"path":"$details"}},{"$replaceRoot":{"newRoot":{"$mergeObjects":["$details","$$ROOT"]}}},{"$facet":{"flavour":[{"$group":{"_id":"$flavour","sum":{"$sum":1}}},{"$addFields":{"flavour":"$_id"}},{"$project":{"_id":0}}],"weight":[{"$group":{"_id":"$weight","sum":{"$sum":1}}},{"$addFields":{"weight":"$_id"}},{"$project":{"_id":0}}]}},{"$addFields":{"flavour":{"$reduce":{"input":{"$filter":{"input":{"$map":{"input":"$flavour","as":"w","in":{"$cond":[{"$ne":["$$w.flavour",null]},{"$let":{"vars":{"o":[["$$w.flavour","$$w.sum"]]},"in":{"$arrayToObject":"$$o"}}},null]}}},"as":"f","cond":{"$ne":["$$f",null]}}},"initialValue":{},"in":{"$let":{"vars":{"d":"$$value","p":"$$this"},"in":{"$mergeObjects":["$$d","$$p"]}}}}},"weight":{"$reduce":{"input":{"$filter":{"input":{"$map":{"input":"$weight","as":"w","in":{"$cond":[{"$ne":["$$w.weight",null]},{"$let":{"vars":{"o":[["$$w.weight","$$w.sum"]]},"in":{"$arrayToObject":"$$o"}}},null]}}},"as":"f","cond":{"$ne":["$$f",null]}}},"initialValue":{},"in":{"$let":{"vars":{"d":"$$value","p":"$$this"},"in":{"$mergeObjects":["$$d","$$p"]}}}}}}},{"$project":{"details":"$$ROOT"}}])

Result