Database

[{"shopId":ObjectId("6043adb043707c034d5363b6"),/** <- this is mongodb id*/"shopItems":[{itemId:1,/** mongodb id*/itemCount:5,colorId:"colorId1"},{itemId:2,/** mongodb id*/itemCount:3,colorId:"colorId2"},{itemId:777,/** mongodb id*/itemCount:999,colorId:"colorId2"},{itemId:777,/** mongodb id*/itemCount:111,colorId:"myColor"}]},{"shopId":ObjectId("604c776763c302032e610347"),/**<- this is mongodb id*/"shopItems":[{itemId:2,/** mongodb id*/itemCount:5,colorId:"colorId1"},{itemId:3,/** mongodb id*/itemCount:3,colorId:"colorId2"}]},{"shopId":ObjectId("60f58c8117514c032774b184"),/** <- this is mongodb id*/"shopItems":[{itemId:3,/** mongodb id*/itemCount:5,colorId:"colorId1"},{itemId:1,/** mongodb id*/itemCount:3,colorId:"colorId1"},{itemId:777,/** mongodb id*/itemCount:6,colorId:"colorId2"}]}]

Query

db.collection.aggregate([{"$match":{}/** <-- Highly recommend you to use match due to the complexity of this query*/},{$group:{_id:0,data:{$push:{shopId:"$shopId",shopItems:"$shopItems"}},shopIds:{"$push":{shopId:"$shopId","count":0}}}},{$unwind:"$data"},{$unwind:"$data.shopItems"},{$group:{_id:{itemId:"$data.shopItems.itemId",colorId:"$data.shopItems.colorId"},data:{$push:{shopId:"$data.shopId",count:"$data.shopItems.itemCount"}},existing:{$push:{shopId:"$data.shopId","count":0}},shopIds:{$first:"$shopIds"}}},{"$addFields":{"missing":{"$setDifference":["$shopIds","$existing"]}}},{$project:{data:{$concatArrays:["$data","$missing"]}}},{$unwind:"$data"},{$sort:{"data.shopId":1}},{$group:{_id:"$_id",counts:{/** here you can change this key*/$push:"$data"},totalCount:{$sum:"$data.count"}/** if you want it*/}}])

Result