Mongo Playground

Configuration

[{"_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([{"$addFields":{"details":{"$map":{"input":"$details","as":"d","in":{"$let":{"vars":{"ar":{"$map":{"input":{"$objectToArray":"$$d"},"as":"m","in":["$$m.k","$$m.v"]}}},"in":{"type":{"$arrayElemAt":[{"$arrayElemAt":["$$ar",0]},0]},"value":{"$arrayElemAt":[{"$arrayElemAt":["$$ar",0]},1]}}}}}}}},{"$unwind":{"path":"$details"}},{"$replaceRoot":{"newRoot":"$details"}},{"$group":{"_id":{"type":"$type","value":"$value"},"sum":{"$sum":1}}},{"$replaceRoot":{"newRoot":{"$mergeObjects":["$_id","$$ROOT"]}}},{"$project":{"_id":0}},{"$group":{"_id":"$type","values":{"$push":{"value":"$value","sum":"$sum"}}}},{"$addFields":{"type":"$_id"}},{"$project":{"_id":0}}])

Result