Try this
const { user } = req;
productsModels.aggregate([
{ $sort: { '_id': -1 } },
{ $limit: 10 },
{
$lookup: {
from: 'likes',
let: {productId:"$_id"},
pipeline: [
{
$match: {
$expr:{$eq:['$_id', '$$productId']}},
'userId': mongoose.Type.Object(user.id)
}
}
],
as: 'liked'
}
},
]);}
In your query two things where missing
1) Converting userid to mongo object id so we used mongoose.Types.ObjectId
2) You can't use outer collection field directly in inner pipeline for this you have create temp variable so we used let
to declare and to match with internal field we need to use $expr