Database

db={"store":[{"_id":"store id 1","storeName":"store one","shopId":"shopId 1","shopItems":[{"_id":"6048a1fa31d779032b16301e","itemId":"111","itemCount":2},{"_id":"6048a46e31d779032b163043","itemId":"222","itemCount":0}]},{"_id":"storeId 2","storeName":"store two","shopId":"shopId 2","shopItems":[{"_id":"6048a1fa31d779032b16301e","itemId":"222","itemCount":2},{"_id":"6048a46e31d779032b163043","itemId":"333","itemCount":-5},{"_id":"6048a46e31d779032b163043","itemId":"111","itemCount":-7}]}],"products":[{"_id":"111","differentPricesForShops":false,"productName":"some name here","buyPrice":10,"sellingPrice":100,"difPriceArray":[]},{"_id":"222","differentPricesForShops":true,"productName":"some name here","difPriceArray":[{"shopId":"shopId 1","buyPrice":5,"sellingPrice":50},{"shopId":"shopId 2","buyPrice":15,"sellingPrice":55}]},{"_id":"333","differentPricesForShops":false,"productName":"some name here","buyPrice":5,"sellingPrice":15,"difPriceArray":[]}],}

Query

db.store.aggregate([{$unwind:{path:"$shopItems"}},{$lookup:{from:"products",localField:"shopItems.itemId",foreignField:"_id",as:"shop_products"}},{$unwind:{path:"$shop_products"}},{$addFields:{buyPrice:{$cond:{if:{$anyElementTrue:["$shop_products.difPriceArray"]},then:{$arrayElemAt:[{$filter:{input:"$shop_products.difPriceArray",as:"item",cond:{$eq:["$$item.shopId","$shopId"]}}},0]},else:"$shop_products.buyPrice"}},sellingPrice:{$cond:{if:{$anyElementTrue:["$shop_products.difPriceArray"]},then:{$arrayElemAt:[{$filter:{input:"$shop_products.difPriceArray",as:"item",cond:{$eq:["$$item.shopId","$shopId"]}}},0]},else:"$shop_products.sellingPrice"}},}},{$addFields:{buyPrice:{$cond:{if:{$eq:[{$type:"$buyPrice"},"object"]},then:"$buyPrice.buyPrice",else:"$buyPrice"}},sellingPrice:{$cond:{if:{$eq:[{$type:"$sellingPrice"},"object"]},then:"$sellingPrice.sellingPrice",else:"$sellingPrice"}}}},{$addFields:{selfprice:{$multiply:[{$ifNull:["$shopItems.itemCount",0]},{$ifNull:["$buyPrice",0]}]},products_for_sale:{$multiply:[{$ifNull:["$shopItems.itemCount",0]},{$ifNull:["$sellingPrice",0]}]},}},{$group:{_id:"$_id",selfprice:{$sum:{$cond:{if:{$lt:["$selfprice",0]},then:0,else:"$selfprice"}}},product_count:{$sum:1},shopId:{$first:"$shopId"},product_in_minus:{$sum:{$cond:{if:{$lt:["$shopItems.itemCount",0]},then:"$shopItems.itemCount",else:0}}},products_for_sale:{$sum:{$cond:{if:{$lt:["$products_for_sale",0]},then:0,else:"$products_for_sale"}}}}}])

Result