MongoDB和mongoos出错

时间:2016-05-02 14:53:37

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

我正在尝试使用Mongoose修改NodeJS中的数组对象的值。我在互联网片段的帮助下编写了以下代码。但是代码似乎并没有起作用。我收到这个错误:我该如何解决?

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"
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

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