Mongoose.js,尝试更新模型时出错

发布时间:2020-07-07 14:58

我在mongodb数据库中使用模块mongoose.js。我的数据库如下所示:

{
  thing1: "thing1",
  thing2: "thing2",
  boolean: false
}

我要对其进行更新,然后放入false。 我尝试过:

const model = await myModel.find({ thing1: "thing1", thing2: "thing2" });
await model.update({ boolean: true });

但是它输出一个错误:

model.update is not a function

我导入了猫鼬,导入了myModel,与数据库的连接很好,我该如何解决?

回答1

model.update仅在直接更新且其第一个参数是查询元素,而下一个是使用$ set运算符的更新值时才能完成。您的代码应如下所示 await myModel.findOneAndUpdate({thing1: "thing1", thing2: "thing2"},{$set:{boolean:true}})