mongodb `$lookup` or `join` with attributes inside array of objects mongodb `$lookup` or `join` with attributes inside array of objects mongoose mongoose

mongodb `$lookup` or `join` with attributes inside array of objects


You need to revert your $map and iterate shaufel_customers instead of customer - this will return two results:

{    $project: {        shaufel_customer_union_id: 1,        customerUnion: '$customerUnions',        shaufel_customers: {            $map: {                input: "$shaufel_customers",                as: "sc",                in: {                    $mergeObjects: [                        "$$c",                        {                            $arrayElemAt: [{                                $filter: {                                    input: "$customers",                                    cond: {$eq: ["$$this._id", "$$sc.customer"]}                                }                            }, 0]                        },                    ]                }            },        }    }},