How to use $lookup $let variables in mongodb? How to use $lookup $let variables in mongodb? mongoose mongoose

How to use $lookup $let variables in mongodb?


You can only convert req.user._id using mongoose.Types.ObjectId as it becomes a constant value for Aggregation Framework. To convert a variable $$content you have to use $toObjectId operator.

{"$lookup": {    "from": "likes",    "let": {'content': "$_id" },    "pipeline": [        {'$match': { $expr: { $and: [             { $eq: [ "$user", mongoose.Types.ObjectId(req.user._id) ] },            { $eq: [ "$content", { $toObjectId: "$$content" } ] },        ] } } }    ],    "as": "liked"}},