Mongoose 更新未更新:{ ok: 0, n: 0, nModified: 0 } [英] Mongoose update not updating: { ok: 0, n: 0, nModified: 0 }

查看:24
本文介绍了Mongoose 更新未更新:{ ok: 0, n: 0, nModified: 0 }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MongoDB 上有一个名为权限"的集合.我想实现一个这样的简单更新:

I have a collection named "permissions" on MongoDB. I want to implement a simple update like this:

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 的控制台日志中.

However I receive { ok: 0, n: 0, nModified: 0 } in console log of docs and null in console log of err.

我做错了什么?

推荐答案

根据 docs

模型是从架构定义编译的奇特构造函数.一个模型的实例称为文档.模型负责从底层 MongoDB 数据库创建和读取文档.

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() 调用期间创建实例.其他操作(更新、读取、删除)应用于现有文档,因此无需创建实例.

So you need to create instance during the .save() call only. Other operations(update, read, delete) applied on the existing document and hence, no need to create instance.

这篇关于Mongoose 更新未更新:{ ok: 0, n: 0, nModified: 0 }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆