Database

[{_id:"ddfdfdfdggfgfgsg",rate:"3323",quantity_packs:"343",shop_name:"Whole Foods",sku:"20",manufacturer_name:"Unilever"},{_id:"ddfdfdfsdsds",rate:"434",quantity_packs:"453",shop_name:"Carrefour",sku:"200",manufacturer_name:"Unilever"},{_id:"dfdfdgcvgfgfvvv",rate:"343",quantity_packs:"23",shop_name:"Target",manufacturer_name:"Beirsdorf",sku:"34"}]

Query

db.collection.aggregate([{$match:{manufacturer_name:{$in:["Unilever"]}}},{"$lookup":{"from":"collection","let":{"id":"$_id"},"pipeline":[{$match:{$expr:{$and:[{$eq:["$_id","$$id"]},{$ne:["$manufacturer_name","unilever"]}]}}},{$group:{_id:{"Shop Name":"$shop_name"},"annual_cost_wo_gst_wo_manu":{$sum:{"$toDouble":"$rate"}},"annual_qty":{$sum:{"$toDouble":"$quantity_packs"}}}},{$project:{"ann_cost_for_other_manufacturers":{$multiply:["$annual_cost_wo_gst_wo_manu","$annual_qty"]},}}],"as":"secondQueryResult"}},{$unwind:"$secondQueryResult"},{$group:{_id:{"Shop Name":"$shop_name"},"total_sku":{"$addToSet":"$sku"},"annual_cost":{$sum:{$cond:[{$eq:["$manufacturer_name","Unilever"]},{"$toDouble":"$rate"},0]}},"annual_qty":{$sum:{"$toDouble":"$quantity_packs"}},"Ann Cost For Other Manufacturers":{$first:"$secondQueryResult.ann_cost_for_other_manufacturers"}}},{$project:{"sku count":{"$size":"$total_sku"},"Annual Cost WO GST":{$multiply:["$annual_cost","$annual_qty"]},"Ann Cost For Other Manufacturers":1}},])

Result