I have following document in mongoDB.
{
"_id": {
"$oid": "5fcca00b7bb43918340afb29"
},
"userEmail": "sairam0730@gmai.com",
"ledgerID": 5,
"ledgerName": "Direct expenses",
"child": [{
"rateOfDuty": 0,
"openingBalance": 0,
"closingBalance": 0,
"_id": {
"$oid": "5fcca00b7bb43918340afb2a"
},
"ledgerID": 5,
"ledgerName": "Direct expenses",
"childID": 4,
"childName": "Labour charges & wages",
"reference": "Daily wages",
"gstApplicable": false,
"transactions": [{
"_id": {
"$oid": "5fccbcba9dd51d1794a94ebc"
},
"transactionID": "5fccbcba9dd51d1794a94eb9",
"date": "06-Dec-2020",
"particulars": "To Cash",
"voucherType": "Cash",
"voucherNumber": "CV-3",
"credit": 0,
"debit": 3000
}]
}, {
"rateOfDuty": 0,
"openingBalance": 0,
"closingBalance": 0,
"_id": {
"$oid": "5fcca0307bb43918340afb2b"
},
"ledgerID": 5,
"ledgerName": "Direct expenses",
"childID": 6,
"childName": "Salaries",
"reference": "Office salaries",
"gstApplicable": false,
"transactions": []
}, {
"rateOfDuty": 0,
"openingBalance": 0,
"closingBalance": 0,
"_id": {
"$oid": "5fccad64032f8924dc2cc5c3"
},
"ledgerID": 5,
"ledgerName": "Direct expenses",
"childID": 6,
"childName": "Salaries",
"reference": "Corp",
"gstApplicable": false,
"transactions": []
}],
"__v": 0
}
I need to push a transaction into transction array of a particular _id inside child object.
Like this,
{
"_id": {
"$oid": "5fcca00b7bb43918340afb29"
},
"userEmail": "sairam0730@gmai.com",
"ledgerID": 5,
"ledgerName": "Direct expenses",
"child": [{
"rateOfDuty": 0,
"openingBalance": 0,
"closingBalance": 0,
"_id": {
"$oid": "5fcca00b7bb43918340afb2a"
},
"ledgerID": 5,
"ledgerName": "Direct expenses",
"childID": 4,
"childName": "Labour charges & wages",
"reference": "Daily wages",
"gstApplicable": false,
"transactions": [{
"_id": {
"$oid": "5fccbcba9dd51d1794a94ebc"
},
"transactionID": "5fccbcba9dd51d1794a94eb9",
"date": "06-Dec-2020",
"particulars": "To Cash",
"voucherType": "Cash",
"voucherNumber": "CV-3",
"credit": 0,
"debit": 3000
}]
}, {
"rateOfDuty": 0,
"openingBalance": 0,
"closingBalance": 0,
"_id": {
"$oid": "5fcca0307bb43918340afb2b"
},
"ledgerID": 5,
"ledgerName": "Direct expenses",
"childID": 6,
"childName": "Salaries",
"reference": "Office salaries",
"gstApplicable": false,
"transactions": [
{
transactionID: "101",
date: "",
particulars: "To ",
voucherType: "Payments",
voucherNumber: "",
debit: 30,
credit: ""
}
]
}, {
"rateOfDuty": 0,
"openingBalance": 0,
"closingBalance": 0,
"_id": {
"$oid": "5fccad64032f8924dc2cc5c3"
},
"ledgerID": 5,
"ledgerName": "Direct expenses",
"childID": 6,
"childName": "Salaries",
"reference": "Corp",
"gstApplicable": false,
"transactions": []
}],
"__v": 0
}
How could I achieve this in MongoDB.
Thank you.
What I have tried:
I've tried this,
await Ledger
.updateOne({ child: { $elemMatch: { _id: mongoose.Types.ObjectId(req.body._id) } } }, {
$push: {
"child.$[inner].transactions": {
transactionID: "shreyas",
date: "",
particulars: "To ",
voucherType: "Payments",
voucherNumber: "",
debit: 30,
credit: ""
}
}
})
.then(async(result) => {
res.status(200).json({
data: result
})
});
});