I have an API in which I am deleting 'course' documents in the following way:
module.exports.deleteCourse = function(req, res){
var courseid = req.params.courseid;
if(courseid){
Course.findByIdAndRemove(courseid, function(err, course){
if(err){
sendJSONResponse(res, 404, err);
return;
}
sendJSONResponse(res, 204, null)
});
} else{
sendJSONResponse(res, 404, {"message":"NoId"});
}
};
This is successful in deleting the course from the database, as is shown when attempting to find it by id.
The issue is that in user documents:
var instructorSchema = new mongoose.Schema({
name: {type: String,
unique: true,
required: true},
password: {type: String,
required: true},
courses: [course.schema]
});
If the document was pushed to the courses array it remains after the deletion method.
So my question. Is there a relatively painless way to keep this document updated after deletes?
Thanks.