I have a nested array "orders" in my collection "customers" array. I want to push value in orders corresponding to particular _id. How to do it using nodejs ?
This is how my array looks like. My collection name is "customers".
{
"_id":"5a696e875dceae1e40da3c00",
"name":"John",
"address":"6lacewood crescent road",
"city":"Toronto",
"state":"Ottawa",
"email":"[email protected]",
"zipcode":"123456",
"gender":"1",
"orders": [
{
"product":"Basket",
"date":"2014-09-13T18:30:00.000Z",
"Quantity":1,
"Unitprice":29.99
},
{
"product":"Basket",
"date":"2014-08-31T18:30:00.000Z",
"Quantity":2,
"Unitprice":29.99
},
{
"product":"Controller",
"date":"2014-08-13T18:30:00.000Z",
"Quantity":3,
"Unitprice":39.99
}
]
I want to push another order in orders array. How to do it using nodejs ?
nodejs
app.post('/addorders:id', function(req, res, next){
console.log(req.params.id);
var body = req.body;
console.log('update command');
dbConn.then(function(db) {
var dd = db.db("customermanagement");
console.log('update command');
dd.collection("customers").findByIdAndUpdate( ObjectId(req.params.id), {$push : {orders : body}}, function(err,doc){
if(err) console.error(err)
console.log('pushed')
});
});
});