MongoDB join/lookup not working when using a pipeline

Viewed 58

using a join in MongoDb I can get the values I want

(await this.ideasCollection).aggregate([
    {
        $match: { userId, deleted: { $ne: true } },
    },
    {
        $lookup: {
            from: 'teams',
            localField: '_id',
            foreignField: `ideas.daily.ideaId`,
            as: 'teams',
        },
    },
])

but when I try to use a pipeline in order to get fewer fields(using $project) it doesn't work

(await this.ideasCollection).aggregate([
    {
        $match: { userId, deleted: { $ne: true } },
    },
    {
        $lookup: {
            from: 'teams',
            let: { ideaId: '$_id' },
            pipeline: [
                { $match: { $expr: { $eq: ['$ideas.daily.ideaId', '$$ideaId'] } } },
                { $project: { _id: 1, name: 1 } },
            ],
            as: 'teams',
        },
    },
])

Is there some kind of trick to make the second option work?

Here's the structure of teams

{
    "_id": { "$oid": "58948652ab5f580010faeb44" },
    "name": "My team",
    "ideas": {
        "daily": [
            { "ideaId": { "$oid": "5b6d913e3e552800260904e1" }, "ordinal": 0 },
            { "ideaId": { "$oid": "5bbbc93459914f0013e3c522" }, "ordinal": 1 },
        ]
    }
}

Thank you

0 Answers
Related