mongoose findOneAndUpdate returns 更新文档但不更新数据库

mongoose findOneAndUpdate returns updated document but doesn't update database

考虑以下猫鼬查询:

mongoose.model(collection).findOneAndUpdate({[fieldName]: fieldValue}, {$set:{processed:1}}, {new:true}, function(err, doc){
    console.log(doc);
    callback(err, doc);
});

这个returns文档虽然跟属性processed:1一样,但是不更新mongodbtable.

我没有在处理过的 mongoose 模式上设置 属性。所以模式会是这样的:

 var mongoose = require('mongoose');

module.exports = mongoose.model('collection', {
    _id:mongoose.Schema.Types.ObjectId,
    parm1:{type: String},
    parm2:{type: Number},
    parm3:{type: String, index:true}
}, 'mycollection');

有没有我可以设置的选项,以便 returns 在更新数据库记录的同时更新文档?

Mongoose 只能使用架构中定义的字段更新 table。因此,如果 Mongoose 模式中不存在 processed 字段,它不会更新 table.

您可以在更新时设置 strict 选项 属性。这将覆盖模式并使您可以添加任何您想要的不在定义模式中的属性。

model.findOneAndUpdate(where, data, { ...options, strict: true });

参见:http://mongoosejs.com/docs/guide.html#strict