猫鼬更新未更新:{ok:0,n:0,nModified:0} [英] Mongoose update not updating: { ok: 0, n: 0, nModified: 0 }
问题描述
我在MongoDB上有一个名为"permissions"的集合. 我想实现一个简单的更新,如下所示:
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 }
和null
在err
的控制台日志中.
However I receive { ok: 0, n: 0, nModified: 0 }
in console log of docs
and null
in console log of err
.
我在做什么错了?
推荐答案
根据 docs
模型是从Schema定义编译的精美构造函数. 一个 模型的实例称为文档 .模特负责 从底层的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.
这篇关于猫鼬更新未更新:{ok:0,n:0,nModified:0}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!