I am trying to update an array in a mongoDB from a node.js program. I am able to modify the array from within node.js, but I can not get the changes to save.
http://pastebin.com/j0Mnf7jP
I think I am doing something very wrong. assistance would be appreciated...
You can update a record, or document as it is called in MongoDB, by using the updateOne() method. The first parameter of the updateOne() method is a query object defining which document to update. Note: If the query finds more than one record, only the first occurrence is updated.
MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.
Change this line:
({_id:doc._id},$set:{scores:zz});
To:
({_id:doc._id}, { $set:{scores:zz}} );
This should also probably be wrapped with a callback, to catch errors:
db.schools.update({_id:doc._id}, {$set:{scores:zz}}, function(err, result) {
if (err)
//do something.
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With