Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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: "" //Amount
                }
        ]
    }, {
        "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: "" //Amount
                }
            }
        })
        .then(async(result) => {
            res.status(200).json({
                data: result
            })
        });
});
Posted
Updated 13-Dec-20 21:28pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900