Database
db={"products":[{_id:"abc",name:"Pizza",attributes:[{salePrice:250,size:"Small"},{salePrice:300,size:"Medium"},{salePrice:350,size:"Large"}]},{_id:"xyz",name:"Burger",attributes:[{salePrice:100,size:"Regular"},{salePrice:150,size:"Large"}]}],"cart":[{cartProducts:[{productId:"xyz",type:"Large",quantity:5},{productId:"abc",type:"Regular",quantity:2}]}]}
Query
db.cart.aggregate([{$unwind:"$cartProducts"},{$lookup:{from:"products",localField:"cartProducts.productId",foreignField:"_id",as:"products"}},{$unwind:"$products"},{$project:{cartProducts:{$mergeObjects:["$cartProducts",{$reduce:{input:"$products.attributes",initialValue:{},in:{$cond:[{$eq:["$$this.sise","$caartProducts.type"]},{name:"$products.name",salePrice:"$$this.salePrice"},"$$value"]}}}]}}}])