猫鼬更新不更新:{ ok: 0, n: 0, nModified: 0 }

Mongoose update not updating: { ok: 0, n: 0, nModified: 0 }

我在 MongoDB 上有一个名为 "permissions" 的 collection。 我想像这样实现一个简单的更新:

let schema = new Schema({
    title: String
  });
  let Permissions = mongoose.model("Permission", schema);
  let permission = new Permissions();

  let query = {};
  let newValues = {
    $set: {
      title: "Yes"
    }
  };
  permission.updateOne(query, newValues, (err, docs) => {
    console.log(err); // null
    console.log(docs); // { ok: 0, n: 0, nModified: 0 }
    if (err) return cast.error(err);
    return cast.ok();
  });

但是我在 docs 的控制台日志中收到 { ok: 0, n: 0, nModified: 0 }nullerr.

的控制台日志中

我做错了什么?

根据 docs

Models are fancy constructors compiled from Schema definitions. An instance of a model is called a document. Models are responsible for creating and reading documents from the underlying MongoDB database.

因此您只需要在 .save() 调用期间创建实例。应用于现有文档的其他操作(更新、读取、删除)因此无需创建实例。