Ich versuche, den Wert eines Array-Objekts in NodeJS mit Mongoose zu ändern. Ich habe den folgenden Code mit Hilfe von Internetschnipsel geschrieben. Der Code scheint jedoch nicht zu funktionieren. Ich bekomme diesen Fehler: Wie kann ich es lösen?Fehler in MongoDB und Mongoos
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" } ]})' }
Dies ist der Code:
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);
}
}
);
}
}
});
}
Dies ist das Schema ist:
doctor: {
_id : ObjectID(571fb65678fcd63c29db423a),
xyz: "sdfs",
appointmentList : [
{patientID:"123", date: "25 MARCH"},
{patientID:"456", date: "26 MARCH"}
]
}
Dies ist die JSON, die ich auf die Update-Methode bin vorbei:
{
"doctorID": "571fb65678fcd63c29db423a",
"appointment": {
"patientID":"123",
"dateAndTime": "TTTTTTTTTTTT"
}
}
Ich erhalte diese Fehlermeldung: '{name: 'MongoError', Nachricht:.. 'Positional Projektion \' appointmentList $ Patienten_ID \ 'nicht die Abfragedokument übereinstimmen', waitedMS: 0, ok: 0, errmsg: 'Positionsprojektion \' terminationList. $. PatiatID \ 'stimmt nicht mit dem Abfragedokument überein.', code: 2} ' – user2498079
Ok, ich habe dieses Problem mit dieser Abfrage behoben:' {_id: docID, 'terminationList.patientID': jsonObject.appointment.patientID}, {'terminationList. $. patientID': jsonObject.appointment.patientID}, 'Das Update funktioniert jedoch immer noch nicht. Gibt es diesen Fehler: 'kann nicht das Teil (Terminliste in Terminliste.PatientenID) verwenden, um das Element durchlaufen ' – user2498079
, damit Sie in der Lage sind, die Daten zu erhalten? –