_id match not working in lookup

I’ve tried converting to string to objectId but its still not working, this is the lookup thats not working:

{
    $lookup: {
        from: "comments",
            let: { refId: "$_id" },
        pipeline: [
            {
                $match: {
                    $expr: {
                        $eq: ["$feed.refId", "$$refId"]
                    }
                }
            },
            {
                $project: {
                    _id: 1,
                    user: 1,
                    content: 1,
                    parent: 1,
                    feed: 1,
                }
            },
        ],
            as: "comments",
    }
},

its a part of full aggregation query, the $_id is of answer. now the comments are still returned empty array which means the match is not working any idea where could i be wrong ?
for reference heres the full query:

const answer = await Question.aggregate([
        {
            $match: {
                questionLink: slug
            }
        },
        {
            $lookup: {
                from: "answers",
                as: "answers",
                let: { questionID: "$_id" },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: ["$questionID", "$$questionID"]
                            }
                        },
                    },
                    {
                        $lookup: {
                            from: "sign-ups",
                            localField: "profileID",
                            foreignField: "_id",
                            as: "profileID",
                            pipeline: [
                                {
                                    $project: {
                                        first_name: 1,
                                        last_name: 1,
                                        profileImg: 1,
                                        email: 1,
                                        further: 1,
                                        points: 1,
                                    }
                                }
                            ]
                        }
                    },
                    {
                        $unwind: "$profileID"
                    },
                    {
                        $lookup: {
                            from: "comments",
                            let: { refId: "$_id" },
                            pipeline: [
                                {
                                    $match: {
                                        $expr: {
                                            $eq: ["$feed.refId", "$$refId"]
                                        }
                                    }
                                },
                                {
                                    $project: {
                                        _id: 1,
                                        user: 1,
                                        content: 1,
                                        likes: 1,
                                        isEdited: 1,
                                        parent: 1,
                                        depth: 1,
                                        feed: 1,
                                    }
                                },
                            ],
                            as: "comments",
                        }
                    },
                ],
            }
        },
        {
            $project: {
                answers: 1,
            }
        }
    ]);

for further ref this is data snap from DB:
COMMENT:

{
  "_id": {
    "$oid": "652ebc783709d2ccaf9102b9"
  },
  "user": {
    "$oid": "617c08cba353ad33ea7ac2dd"
  },
  "feed": {
    "type": "answers",
    "refId": {
      "$oid": "65295a12d1e2dfea76ad4933"
    }
  },
  "parent": null,
}

ANSWER:

{
  "_id": {
    "$oid": "62baeacf9bec5b79db41b189"
  },
  "userID": "62b58a02a44aca7c8a89a888",
  "questionID": {
    "$oid": "62bae9639bec5b79db41b174"
  },
  "answer": "xyz"
  "dateTime": "1656416975346",
  "profileID": {
    "$oid": "62b58a02a44aca7c8a89a888"
  },
}

Your sample documents from the answers and comments do not match your use-case. The _id from answers do not match feed.refId in *comments.

When your $match inside a $lookup simply uses $eq on the fields, you might be better off using localField: and foreignField: just like you do with your $lookup from: sign-ups.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.