Database
[{"collection":"users","count":10,"content":{"_id":{"type":"reference","id":1,"refContent":{"type":"objectId"}},"firstName":{"type":"faker","method":"FirstName"},"lastName":{"type":"faker","method":"LastName"},"email":{"type":"faker","method":"Email"}}},{"collection":"products","count":100,"content":{"_id":{"type":"reference","id":2,"refContent":{"type":"objectId"}},"title":{"type":"faker","method":"BeerName"},"price":{"type":"int","min":100,"max":500}}},{"collection":"Order","count":3,"content":{"user":{"type":"ref","id":1},"orderItems":{"type":"array","minLength":1,"maxLength":5,"arrayContent":{"type":"object","objectContent":{"product":{"type":"ref","id":2},"quantity":{"type":"int","min":1,"max":5}}}}}}]
Query
db.Order.aggregate([{/** get user details*/"$lookup":{"from":"users","localField":"user","foreignField":"_id","as":"user"}},{/** get all product details*/"$lookup":{"from":"products","localField":"orderItems.product","foreignField":"_id","as":"products"}},{"$project":{"user":1,"products":{"$map":{"input":"$products","as":"product","in":{/** format each object as desired* redundant _id*/"_id":"$$product._id",/** full product details*/"productId":"$$product",/** get quantity for this product*/"quantity":{"$getField":{"field":"quantity","input":{"$first":{"$filter":{"input":"$orderItems","as":"item","cond":{"$eq":["$$item.product","$$product._id"]}}}}}}}}}}}])