Database

[{"item":"Ground Meat","type":"junk","ingredients":{"cheese":"120g","meat":"70g"}},{"item":"Chicken","type":"junk","ingredients":{"cheese":"100g","meat":"60g"}},{"item":"Burger","type":"junk","ingredients":{"cheese":"120g","meat":"50g"}},{"item":"Green Spanich","type":"healty","ingredients":{"Spinach":"12g","Leaves":"1.5g",}},{"item":"Pumpkin Salad","type":"healty","ingredients":{"Spinach":"18g","Leaves":"1.2g",}}]

Query

db.collection.aggregate([{"$set":{"ingredients":{"$objectToArray":"$ingredients"}}},{"$unwind":"$ingredients"},{"$group":{"_id":{type:"$type",ingredient:"$ingredients"},"count":{"$sum":1},"doc_count":{"$addToSet":"$item"}}},{"$group":{"_id":{type:"$_id.type",ingredient:"$_id.ingredient.k"},"docs":{"$push":{k:"$_id.ingredient.v",v:"$count"}},"doc_count":{"$push":"$doc_count"}}},{"$group":{"_id":"$_id.type","ingredients":{"$push":{k:"$_id.ingredient",v:{"$arrayToObject":"$docs"}}},"doc_count":{"$push":{$reduce:{input:"$doc_count",initialValue:[],in:{$concatArrays:["$$value","$$this"]}}}}}},{"$set":{"ingredients":{"$arrayToObject":"$ingredients"},doc_count:{"$size":{"$setUnion":{$reduce:{input:"$doc_count",initialValue:[],in:{$concatArrays:["$$value","$$this"]}}}}}}}])

Result