Mongo Playground

Configuration

db={"carts":[{"_id":ObjectId("62f8086e29c549f34ab89df7"),"user":ObjectId("62f3f8600e93c17d1c25c2ed"),"product":[ObjectId("62f391b9482a375c4f83de8e"),ObjectId("62f39121482a375c4f83de8d"),ObjectId("62f39200482a375c4f83de8f")]}],"products":[{"_id":ObjectId("62f39121482a375c4f83de8d"),"name":"iphone 15","category":"mobiles","price":"125","description":"fastest iphone"},{"_id":ObjectId("62f391b9482a375c4f83de8e"),"name":"OnePlus Nord","category":"mobile","price":"40000","description":"budget phone of OnePlus"},{"_id":ObjectId("62f39200482a375c4f83de8f"),"name":"Samsung M33","category":"mobile","price":"25000","description":"mid range mobile"}]}

Query

db.carts.aggregate([{"$match":{"user":ObjectId("62f3f8600e93c17d1c25c2ed")}},{"$lookup":{"from":"products","as":"cartItems","let":{"prodList":"$product"},pipeline:[{"$match":{"$expr":{"$in":["$_id","$$prodList"],},},},],}}])

Result