使用MongooseJS更新文档中的多个字段

Yi *_*i Z 3 mongoose

假设我有一个如下所示的ABC模式:

ABC = mongoose.Schema ({
    name: String
    , x: String
    , y: String
    , z: String
});
Run Code Online (Sandbox Code Playgroud)

如果名称匹配,我想更新字段x和y。MongooseJS的“更新” API有可能吗?

我尝试了以下操作,但没有成功:

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();
    }
    ...
});
Run Code Online (Sandbox Code Playgroud)

即使有可能,是否最好只更新ABC对象并改用ABC.save(abc)?我在另一个线程中读到,更新比保存好,因为它是保护程序。真的吗?

任何建议表示赞赏!

ale*_*ck3 5

是的,您可以更新多个字段,您只需要执行以下操作

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