Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update a mongodb document from node.js?

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...

like image 378
IrishGringo Avatar asked Sep 02 '13 03:09

IrishGringo


People also ask

How do you update a specific field in MongoDB using node JS?

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.

Can we update MongoDB?

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.


1 Answers

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.
});
like image 165
tymeJV Avatar answered Oct 11 '22 18:10

tymeJV