I'd like to replace an object in an array using an index, but nothing will save. This is what the document looks like:
{
"_id": {
"$oid": "58a71ec0c80a9a0436ae2fb1"
},
"owner": "[email protected]",
"contacts": [
{
"work": "",
"home": "",
"mobile": "",
"email": "",
"company": "",
"last": "Contact",
"middle": "",
"first": "New"
},
{
"first": "Another",
"middle": "",
"last": "Contact",
"company": "",
"email": "",
"mobile": "",
"home": "",
"work": ""
}
],
"__v": 1
}
And this is what I've tried..
Contacts.findById({_id: "58a71ec0c80a9a0436ae2fb1"}, function(err,document) {
document.contacts[req.body.indexOfObjectToBeEdited] = req.body.updatedObject
console.log(document)
document.save(function(err) {
return res.json({event:"Updated Contact"})
})
})
Right before document.save() I console.log(document) and it reflects the correct changes. But when I save, nothing is updated in the mongodb and I receive no errors. What should I be doing differently?