2016-05-06 14 views
0

Ich versuche, ein JSON-Objekt in einem Array mit dem folgenden Code zu aktualisieren. Der Code scheint das JSON-Objekt im Array zu finden, jedoch kann das JSON-Objekt im JSON-Array nicht aktualisiert werden. Es gibt keinen Fehler, deshalb ist es verwirrender.Wie bekomme ich ein Json-Objekt in JsonArray mit Mongoose

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}, {'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, pushedData) { 
         if(err) { 
          console.error("error in adding", err); 
          isDatabaseOperationSuccessful(false, pushedData); 
         } 
         else { 
          console.log("adding successful", pushedData, "inserted: ", jsonObject.appointment); 
          isDatabaseOperationSuccessful(true, pushedData); 
         } 
        } 
       ); 
      } 
      // since that appointment already exists, update it 
      else { 
       foundData.update({'_id':docID,'doctors.appointmentList.patientID' : jsonObject.appointment.patientID}, {$set: {'doctors.appointmentList.$.dateAndTime': jsonObject.appointment.dateAndTime}}, 
        function(err, updatedData) { 
         if (err) { 
          console.error("error in updating", err); 
          isDatabaseOperationSuccessful(false, foundData); 
         } 
         else { 
          if (!updatedData) { 
           console.log("updating failed", updatedData); 
           isDatabaseOperationSuccessful(true, foundData); 
          } 
          else { 
           console.log("updating successful", updatedData); 
           isDatabaseOperationSuccessful(true, foundData); 
          } 
         } 
        } 
       ); 
      } 
     } 
    }); 
} 

Schema:

doctorSchema = mongoose.Schema({ 
     name : String, 
     appointmentList : Array // array of jsonObjects of dates and time 
    }); 

Daten, die ich addOrUpdateAppointment bin vorbei(),

{ 
    "docID": "id assigned by mongoDB", 
    "appointment": { 
     "patientID": "id assigned by mongoDB", 
     "dataAndTime": "IIII" 
    } 
} 

Antwort

0

Ihr Code fast richtig ist nur foundData mit DoctorModel ändern, wenn Sie die Daten aktualisieren möchten . So wird der Code:

  else { 
DoctorModel.update({'_id':docID, 'appointmentList.patientID' : jsonObject.appointment.patientID}, {$set: {'appointmentList.$': jsonObject.appointment}}, 
        function(err, updatedData) {....});