【问题标题】:Error in MongoDB and mongoosMongoDB和猫鼬中的错误
【发布时间】:2016-05-02 14:53:36
【问题描述】:

我正在尝试使用 Mongoose 在 NodeJS 中修改数组对象的值。我在 Internet sn-ps 的帮助下编写了以下代码。但是代码似乎不起作用。我收到此错误:我该如何解决?

error in updating { name: 'MongoError',
  message: 'cannot use the part (appointmentList of appointmentList.patientID) to traverse the element ({appointmentList: [ { patientID: "99999999", dateAndTime: "XXXXX" } ]})',
  driver: true,
  index: 0,
  code: 16837,
  errmsg: 'cannot use the part (appointmentList of appointmentList.patientID) to traverse the element ({appointmentList: [ { patientID: "99999999", dateAndTime: "XXXXX" } ]})' }

这是代码:

function addOrUpdateAppointment(jsonObject, isDatabaseOperationSuccessful) {
    var docID = jsonObject.doctorID; // this is _id from db sent to the doctor upon logging in
    console.log("jsonPssed: ", {_id : docID});
    DoctorModel.findOne({_id : docID, 'appointmentList.patientID': jsonObject.appointment.patientID},function(err, foundData) {
        console.log("found data", foundData);
        if(err) {
            console.error("error in find doctor for adding the appointment", err);
            isDatabaseOperationSuccessful(false, foundData);
            return;
        }
        else {
            // since no document matched your query, add the appointment
            if (!foundData) {
                DoctorModel.update(
                    {_id: docID},
                    {$push: {appointmentList: jsonObject.appointment}},
                    function(err) {
                        if(err) {
                            console.error("error in adding", err);
                            isDatabaseOperationSuccessful(false, foundData);
                        }
                        else {
                            console.log("adding successful");
                            isDatabaseOperationSuccessful(true, foundData);
                        }
                    }
                );
            }
            // since that appointment already exists, update it
            else {
                foundData.update(
                    {_id: docID, 'appointmentList.patientID' : jsonObject.appointment.patientID},
                    {$set: {'appointmentList.$.appointment': jsonObject.appointment}},
                    function(err, updatedData) {
                        if (err) {
                            console.error("error in updating", err);
                            isDatabaseOperationSuccessful(false, foundData);
                        }
                        else {
                            console.log("updating successful", updatedData);
                            isDatabaseOperationSuccessful(true, foundData);
                        }
                    }
                );
            }
        }
    });
}

这是架构:

doctor: {
   _id : ObjectID(571fb65678fcd63c29db423a),
   xyz: "sdfs",
   appointmentList : [
    {patientID:"123", date: "25 MARCH"},
    {patientID:"456", date: "26 MARCH"}
   ]
}

这是我传递给更新方法的 JSON:

{
    "doctorID": "571fb65678fcd63c29db423a",
    "appointment": {
      "patientID":"123",
        "dateAndTime": "TTTTTTTTTTTT"
    }
}

【问题讨论】:

    标签: node.js mongodb mongoose crud mean-stack


    【解决方案1】:

    我认为您应该能够执行以下操作:

        DoctorModel.findOne({_id : docID } , {'appointmentList.$.patientID': jsonObject.appointment.patientID},function(err, foundData) {
           console.log("found data", foundData);
        });
    

    这将首先搜索医生 ID 为 docID 的所有文档,然后为该患者 ID 预约。

    【讨论】:

    • 我收到此错误:{ name: 'MongoError', message: 'Positional projection \'appointmentList.$.patientID\' does not match the query document.', waitedMS: 0, ok: 0, errmsg: 'Positional projection \'appointmentList.$.patientID\' does not match the query document.', code: 2 }
    • 好的,我已经用这个查询解决了这个问题:{_id : docID, 'appointmentList.patientID': jsonObject.appointment.patientID}, {'appointmentList.$.patientID': jsonObject.appointment.patientID}, 但是更新仍然无法正常工作。给出此错误:cannot use the part (appointmentList in appointmentList.patientID) to traverse the element
    • 所以你可以得到数据?
    • 是的。找到了所需的对象,但我无法更新数据。
    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 2014-01-27
    • 2017-10-08
    • 2017-06-30
    • 1970-01-01
    • 2013-12-03
    • 2019-09-04
    • 1970-01-01
    相关资源
    最近更新 更多