简体   繁体   English

MongoDB:在删除文档后更新子文档数组

[英]MongoDB: Updating arrays of sub documents after document deletion

I have an API in which I am deleting 'course' documents in the following way: 我有一个API,可以通过以下方式删除“课程”文档:

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. 尝试从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. 谢谢。

Add a class method for course using statics, where you delete both the course and its dependencies. 使用静态方法为课程添加类方法,在该方法中,您将删除课程及其依赖项。

Assuming you are storing ids in courses array: 假设您将ID存储在courses数组中:

var Instructor = require('./instructor');

courseSchema.statics = {
    removeOneWithDependencies : function(id, done){
        this.findByIdAndRemove(id, function(err, course){
            if(err){
                return done(err);
            }
            else{
                //Removes the course id from courses array of all instructor docs
                Instructor.update({courses: course._id}, { $pullAll: {courses: [course._id] } }, {multi: true}, function(err){ //http://stackoverflow.com/a/27917378/
                    if(err){
                        return done(err);
                    }
                    else{
                        return done();
                    }
                })
            }
        });
    }
}

In case you are storing course documents in courses array you need to change the update query to: 如果将课程文档存储在courses数组中,则需要将更新查询更改为:

Instructor.update({"courses._id": course._id}, { $pull: {courses:{_id: course._id} } }, {multi: true}, function(err){ //http://stackoverflow.com/a/15122017/

Finally use the above method in your API: 最后在您的API中使用上述方法:

module.exports.deleteCourse = function(req, res){
    var courseid = req.params.courseid;
    if(courseid){
        Course.removeOneWithDependencies(courseid, function(err){
            if(err){
                return sendJSONResponse(res, 500, err);
            }
            else{
                return sendJSONResponse(res, 204, null);
            }
        });
    } else{
        sendJSONResponse(res, 404, {"message":"NoId"});
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM