Ternyata casting ObjectId sepertinya menjadi masalah. Kita perlu menggunakan mongoose.Types.ObjectId
Kode API akan terlihat seperti ini
getProductByProductId: function (productId) {
return new Promise((resolve, reject) => {
User.aggregate([
{
$match: {
"shops.products._id": mongoose.Types.ObjectId(productId)
}
},
{
"$unwind": "$shops"
},
{
"$unwind": "$shops.products"
},
{
$match: {
"shops.products._id": mongoose.Types.ObjectId(productId)
}
},
{
$project: {
"_id": "$shops.products._id",
"title": "$shops.products.title"
}
}
])
.then(products => {
resolve(products)
}).catch(err => {
reject(err);
});
});
}