Database
db={"productTypes":[{"_id":"609d79de5909592f2635c64e","name":"T-Shirt","subType":"Round Neck","__v":0,"size":"XXL","sellingPrice":320,"createdAt":ISODate("2021-05-18T05:22:00.695Z"),"actualPrice":200,"updatedAt":ISODate("2021-05-25T12:11:50.986Z")},{"_id":"609d79de5909592f2635c64d","name":"T-Shirt","subType":"V Neck","__v":0,"size":"XXL","sellingPrice":290,"createdAt":ISODate("2021-05-18T05:22:00.695Z"),"actualPrice":200,"updatedAt":ISODate("2021-05-25T12:11:50.986Z")}],"orders":[{"_id":"60a63e369cf3a806c0209bd8","items":[{"type":"609d79de5909592f2635c64e","quantity":1,"sellingPrice":320},{"type":"609d79de5909592f2635c64d","quantity":2,"sellingPrice":290}],"orderId":"ORD101","from":"Abc","to":"xyz","createdAt":ISODate("2021-05-20T10:47:18.920Z"),"__v":0,"tracking":"12345678"}]}
Query
db.orders.aggregate([{$project:{orderId:1,createdAt:1,items:1}},{$unwind:"$items"},{$lookup:{from:"productTypes",localField:"items.type",foreignField:"_id",as:"item"}},{$addFields:{profit:{$multiply:[{$subtract:["$items.sellingPrice",{$arrayElemAt:["$item.actualPrice",0]}]},"$items.quantity"]}}},{$group:{_id:"$_id",orderId:{$first:"$orderId"},createdAt:{$first:"$createdAt"},profit:{$sum:"$profit"}}}])