繁体   English   中英

猫鼬更新不更新文档

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM