Mongoose 不更新文档

问题描述 投票:0回答:1

我尝试到处搜索,但总是出现错误或没有任何变化。 这是我的代码:

Blog.findOneAndUpdate({title: "Lol"}, {$push: {details:"detail"}})
.then(function(){
    console.log("Data updated");
}).catch(function(error){
    console.log(error);
});

如果有人能给我建议,我会很高兴。我仍然需要从文档中删除并向文档添加/更新。

(对不起英语...谷歌翻译)

node.js arrays mongodb mongoose mongoose-schema
1个回答
0
投票

这是异步等待版本:

upsert: false(如果没有文档匹配过滤器,请勿创建新文档)

new:true(如果有则返回更新的文档)

let filter = { title: "Lol" };
let update = { $push: { details: "detail" } };
let options = { upsert: false, new: true };
//
try {
  let updated = await Blob.findOneAndUpdate(filter, update, options).exec();
  console.log(updated);
} catch (error) {
  //handle error here
}

© www.soinside.com 2019 - 2024. All rights reserved.