Updating multiple fields in a document with MongooseJS

Let's say I have an ABC diagram that looks like this:

ABC = mongoose.Schema ({
    name: String
    , x: String
    , y: String
    , z: String
});

      

I want to update the x and y fields if the name is the same. Is this possible with MongooseJS API?

I tried the following and didn't work:

ABC = mongoose.createConnection(uri).model('ABC', ABC)
...
ABC.findOne({'name': abc.name}).exec(function(err, found) {
    if(found) {
        ABC.update({'name':abc.name}, {'x':abc.x, 'y':abc.y}).exec();
    }
    ...
});

      

Even if possible, is it better to just update the ABC object and use ABC.save (abc) instead? I read on another thread that updating is better than saving because it is stored. It's true?

Any advice is appreciated!

+3


source to share


1 answer


Yes you can update multiple fields, you just need to do something like this



ABC.update({ name: 'whatever' }, {
    x: 'value1',
    y: 'value2'
},
function(err, data) {
    if (err) {
    } else if (!data){
    } else {
        return res.send(200, data);
    }
});

      

+3


source







All Articles