I am trying to find whether a user is liked a product or not while fetching the products from a collection using $lookup operation to search for like which is in a likes collection consists of two fields productId(likedProduct) and userID(who liked the product) both productId and userId are of type ObjectId this is my schema for products
const products = new mongoose.Schema({
created: {
type: Date,
default: Date.now()
},
images: [{
type: String
}]
});
and likes Schema
new Schema({
userId: {
type: Schema.Types.ObjectId,
required: true,
index: true
},
productId: {
type: Schema.Types.ObjectId,
required: true,
index: true,
ref: 'products'
},
created: {
type: Date,
default: Date.now()
}
});
I have tried using $lookup operator as
const { user } = req;
productsModels.aggregate([
{ $sort: { '_id': -1 } },
{ $limit: 10 },
{
$lookup: {
from: 'likes',
pipeline: [
{
$match: {
'productId': '$_id',
'userId':user.id
}
}
],
as: 'liked'
}
},
]);}
but it's not giving me the expected result even though the user has liked the product and I got liked as an empty array!
{
created: "2019-08-30T10:46:53.692Z"
images: ["8f3447bb-8bb8-4c5a-acde-bcdc1b729c09"]
liked: []
price: 9
title: "developer"
userId: "5d68fea345353e1748cf7a10"
__v: 0
_id: "5d68fec745353e1748cf7a12"}
Thanks in advance