【发布时间】:2016-05-05 17:11:06
【问题描述】:
在猫鼬中,我们可以使用model.update()检查更新操作是否修改了文档:
model.update(query, update, function(err, raw){
if (raw.nModified >= 1) console.log('document is modified!')
});
有没有办法对model.findOneAndUpdate() 做同样的事情?
model.findOneAndUpdate(query, update, { new: true }, function(err, doc){
if (doc) {
// So MongoDB found the document, but is there a way
// to know the document was indeed modified?
}
});
【问题讨论】:
-
还有一个选项
upsert: bool - creates the object if it doesn't exist. defaults to false.所以如果它为false,并且mongodb找到了文档,回调函数中的doc总是修改后的文档 -
您可以查看answer
标签: node.js mongodb mongoose database