Reputation: 3
I have an application where I have a database of instructors with an array of ID's for classes. When a new class is created, I'm trying to find the instructor's database entry and update the classID's array with the new class's ID. However, when I try and update it, nothing happens.
This is my code:
data.classID = id;
new course(data).save((error) => {
if(error){
console.log('oops! Could not save course');
} else {
conn3.close();
}
});
//update instructor's classID's variable in intructor database
instructor.findOne({"instructorEmail":data.Instructor}, (err,x)=>{
var arr = x.classIDs;
arr.push(id);
instructor.findOneAndUpdate({"instructorEmail":data.Instructor},{$set:{classIDs: arr}});
})
Upvotes: 0
Views: 170
Reputation: 101
If you are just trying to append an id to the classIDs array you can use the $push operator.
instructor.findOneAndUpdate(
{ "instructorEmail": data.Instructor },
{ $push: { classIDS: id } },
{ new: true },
(err, updatedDoc) => {
// what ever u want to do with the updated document
})
Upvotes: 1