Hello @Yilmaz_Durmaz,
Here don’t need lookup with the pipeline, you can pass an array of ids in localFields property,
Yes, You can use another approach without $unwind and $group stages,
-
$mapto iterate loop ofcartarray -
$filterto iterate loop of items array and find the specific_id’s item -
$firstto get the first element as an object from the above-filtered result, the alternate option is$arrayElemAtif you are using lower version of the MongoDB -
$mergeObjectsto merge the currentcartobject with the above-filtereditemproperty, the alternate option is you can specify the properties name instead of using this operator - You can remove the
itemsarray by using$$REMOVEcommand because it’s not needed in the result, if you are using$projectstage then don’t need this operation.
db.user.aggregate([
{
"$lookup": {
"from": "items",
"localField": "cart._id",
"foreignField": "_id",
"as": "items"
}
},
{
"$addFields": {
"cart": {
"$map": {
"input": "$cart",
"in": {
"$mergeObjects": [
"$$this",
{
"item": {
"$first": {
"$filter": {
"input": "$items",
"as": "i",
"cond": { "$eq": ["$$i._id", "$$this._id"] }
}
}
}
}
]
}
}
},
"items": "$$REMOVE"
}
}
])