node.js 猫鼬 findOneAndUpdate 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15123182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:42:55  来源:igfitidea点击:

Mongoose findOneAndUpdate not working

node.jsmongoose

提问by lostintranslation

I am trying to use 'findOneAndUpdate' in mongoose and the updated JS object I am sending is not getting saved to mongo. I do not get an error upon saving, but I do get back a null for the updated object. Any ideas what I might be doing wrong? This is example is trying to update the entire object as stored in mongo, i.e. overwrite the name object.

我正在尝试在 mongoose 中使用“findOneAndUpdate”,并且我发送的更新的 JS 对象没有保存到 mongo。保存时我没有收到错误消息,但我确实为更新的对象返回了一个空值。任何想法我可能做错了什么?这是一个示例,试图更新存储在 mongo 中的整个对象,即覆盖名称对象。

var query = {"_id": id};
var update = {name: {first: 'john', last: 'smith'}};
var options = {new: true};
People.findOneAndUpdate(query, update, options, function(err, person) {
  if (err) {
    console.log('got an error');
  }

  // at this point person is null.
});

采纳答案by lostintranslation

Turns out that the id I was searching for did not exist, hence the null return. Works as expected!

原来我要搜索的 id 不存在,因此返回空值。按预期工作!