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([{$project:{_id:0,details:1}},{$unwind:"$details"},{$project:{details:{$objectToArray:"$details"}}},{$unwind:"$details"},{$group:{_id:"$details.k",v:{$addToSet:"$details.v"}}},{$group:{_id:null,details:{$push:{k:"$_id",v:"$v"}}}},{$project:{_id:0,details:{$arrayToObject:"$details"}}}])

Result