Javascript mongodb update

How to update mongodb collection from JS?

db.collection('fruits', function (err, collection) {
    collection.update({ "_id": content.fruitID}, content, function () {
    });
});

Am I doing it right? Do I need to use $ set?

Basically, content is a json object that should replace the found document.

Thank.

I am using my own mongodb node driver.

+3
source share
1 answer

If you want to replace a document, you are doing the right thing. If you want to update the field of an existing document, you must use $ set.

See here: http://www.mongodb.org/display/DOCS/Updating

+2
source

Source: https://habr.com/ru/post/1789900/


All Articles