0

Hi i am new in nodejs i need to update a value in nested array using _id of document my database document is look like this..

"complaints" : [ 
    {
        "complaint" : "head light is not working",
        "complaintid" : ObjectId("57205219a56d2b8c0f9274a4"),
        "_id" : ObjectId("57454c9249218eb40c1c0d1f"),
        "labour" : 350,
        "partCost" : 0,
        "part" : [ 
            {
                "id" : ObjectId("56f12eaab915bd9800272ed7"),
                "estimate" : 450,
                "partname" : "clutch",
                "_id" : ObjectId("57454cef49218eb40c1c0d25"),
                "quantity" : 0,
                "qrcodes" : []
            }, 
            {
                "id" : ObjectId("56f12eaab915bd9800272ed7"),
                "estimate" : 450,
                "partname" : "rear tyre",
                "_id" : ObjectId("57454cef49218eb40c1c0d24"),
                "quantity" : 0,
                "qrcodes" : []
            }
        ],
        "acceptance" : true,
        "inspection" : false,
        "color" : "#8929A9",
        "status" : "APPROVED",
        "estimate" : 1200,
        "solution" : "HEAD LIGHT CHANGE",
        "problems" : "HEAD LIGHT IS NOT WORKING"
    }, 

i need to update quantity value of part array exist inside the part array using _id of part array

i am trying this but its not working what should i do for update this value...

var partdata = req.payload.parts;
                for(var k = 0; k< partdata.length ; k++ ){
                    CPS.update({
                        'complaints.part._id' : partdata[k].partid
                    }, {
                        "$inc" : {                              
                            'complaints.part.$.quantity' : partdata[k].quantity                             
                        }
                    }).exec
                    (function(err,temp) {
                        if(err){
                            res(err).code(500);
                        }else{
                            console.log(temp);
                        }
                    });
                }
Manish chauhan
  • 55
  • 1
  • 2
  • 10

1 Answers1

0

MongoDB doesn't support matching into more than one level of an array. Consider altering your document model so each document represents an operation, with information common to a set of operations duplicated in the operation documents.

Following is not the solution for your case. But in-case you know the index then you could do something like this: Assume a sample document like:

{
    "_id" : ObjectId("57454c9249218eb40c1c0d1f"),
    "part" : [{ "quantity" : 111 }, { "quantity" : 222 }]
}

Then this query should work.

db.test.update({ "_id" : ObjectId("57454c9249218eb40c1c0d1f") }, { "$set" : { "part.1.quantity" : 999 } })

Document will get modified as follows :

{
    "_id" : ObjectId("57454c9249218eb40c1c0d1f"),
    "array" : [{ "quantity" : 222 }, { "quantity" : 999 }]
}


Update: You can try following way of doing the update. But its not recommended way of doing probably you need to restructure your schema.
db.test.aggregate([     
    { "$unwind": "$complaints" },     
    { "$unwind": "$complaints.part" },
    { "$project":
        { 
            _id: "$complaints.part._id",
            partqty: "$complaints.part.quantity"
        } 
    },
]);

This should return as follows:

{
    "_id" : ObjectId("57454cef49218eb40c1c0d25"),
    "partqty" : 111
}
{
    "_id" : ObjectId("57454cef49218eb40c1c0d24"),
    "partqty" : 222
}

Now you can use this information to update, e.g

var cur =  db.test.aggregate([     
        { "$unwind": "$complaints" },     
        { "$unwind": "$complaints.part" },
        { "$project":
            { 
                _id: "$complaints.part._id",
                partqty: "$complaints.part.quantity"
            } 
        },
    ]);

    while (cur.hasNext()) {
        var doc = cur.next();
        //Note the index should be know again :|
        db.test.update({ "complaints.part._id":  ObjectId("57454cef49218eb40c1c0d25") },
                      { "$set": { "complaints.$.part.1.quantity": 55 }},
                      { "multi": true})
    }
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164