簡體   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