Using mongoose in express js I want to join above collection by payment.payment_id from Product model with _id (object ID) from payments.
Collection name : products
Model : Product
{
"_id": {
"$oid": "6651b2414bea62ba5a881322"
},
"active": true,
"payment": {
**"payment_id": "6651b2414bea62ba5a881366",**
"accountNo": ""
}
}
Collection name : payments
Model : Payment
{
"_id": {
**"$oid": "6651b2414bea62ba5a881366"**
},
"card": "MOBILE",
"issuer": "XYZ"
}
here is my code
const Info = await Product.aggregate([
{ $match: { _id: mongoose.Types.ObjectId(prdId) } },
{
$lookup: {
from: 'payments',
localField: 'payment.payment_id',
foreignField: '_id',
as: 'paymentDetails'
}
},
{ $unwind: '$paymentDetails' },
{
$project: {
_id: 1,
active: 1,
'payments.card': 1,
'products.active': 1
}
}
]);
But it shows empty value. PLease note I have related data
I want to join two collection using mongoose .