[英]Updating embedded document Mongoose - existing instance save/update etc
[英]Mongoose update not updating document
我正在尝试通过Mongoose更新MongoDB中的文档,但由于某种原因,找不到所需的文档...
Call.update({_id: data.call._id}, { $set: { approved: value }}, function (error, response) {
console.log(error);
console.log(response);
if (error) {
callback(error, null);
} else {
callback(null, response);
}
});
data.call._id
包含有效的文档_id
并且approved
的类型为Number
。
response
输出以下内容:
{ ok: 1, nModified: 0, n: 0, lastOp: { _bsontype: 'Timestamp', low_: 0, high_: 0 }, electionId: 56a92375bfb5c1abc4e825a7 }
文档永远不会在数据库中更新... value
将根据早期情况设置为1
或-1
。
模型:
const callSchema = new Schema({
start: Date,
duration: String,
callerNumber: String,
callerNumberAreaCode: String,
trackingNumber: String,
destinationNumber: String,
answered: String,
customerId: String,
approved: Number
}, { collection: 'calls'});
const Call = mongoose.model('Call', callSchema);
在Model.update中,您不需要使用$set
。 尝试这个:
Call.update({_id: data.call._id}, { approved: value }, function (error, response) {
console.log(error);
console.log(response);
if (error) {
callback(error, null);
} else {
callback(null, response);
}
});
希望这可以帮助某人。
确保您的Scheme具有一个与$set
子句中的FIELD完全正确的FIELD。
例如:
如果您的更新是(设置address
字段):
User.update({_id: mongoose.Types.ObjectId("<doc_id>")}, { $set: { address: "New York" }}, function (err, res) {
if (err) {
console.log("err:", err)
} else {
console.log("success with response:", res)
}
})
您的方案需要包含address
字段,例如:
let User = new mongoose.Scheme({
name: String,
address: String
})
谢谢阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.