The userlist collection contains documents of the following format.
{
"_id" : ObjectId("5381d32ce72cc794166eede2"),
"name" : "Haseeb",
"password" : "dgkhan",
"email" : "hasseeb@yahoo.com",
"address" : "237 D, Faisal Town , Lahore",
"phone" : "5162806"
}
I intend to add another member in the existing document such that the resultant document looks like this.
{
"_id" : ObjectId("5381d32ce72cc794166eede2"),
"name" : "Haseeb",
"password" : "dgkhan",
"email" : "hasseeb@yahoo.com",
"address" : "237 D, Faisal Town , Lahore",
"phone" : "5162806",
"purchases" : [{
"itemID": xyz,
"quantity": 142
},
{
"itemID": kjh,
"quantity": 987
}
}]
}
For this I have written the following mongoskin query, but it is not performing any updates.
db.collection('userlist').update(
{_id:req.session._id},
{
'$push': { purchases: {
itemID: item.ID,
quantity: item.quantity
}
}
}, function(err) {
if (err) throw err;
console.log('Updated!');
});
}
Value of req.session._id = 5381d32ce72cc794166eede2 i.e a valid _id field of a docuemnt in my collection whereas item.ID and item.quantity are also valid strings. Any help would be really appreciated.
I'm more familiar with Mongoose, but looking at the docs you could probably either use:
collection.updateById(req.session._id, ...)
or
collection.update({_id: toObjectID(req.session_id)}, ...)