删除一对一和一对多引用-猫鼬 [英] Removing one-one and one-many references - Mongoose

查看:137
本文介绍了删除一对一和一对多引用-猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Assignment schema,其中引用了GroupsProjects.

I have an Assignment schema which has references to Groups and Projects.

Assignment == Group [One-One Relationship]
Assignment == Projects [One-Many Relationship]

下面是我的Asssignment模式

var AssignmentSchema = new Schema({
    name: String,
    group: {
        type: Schema.Types.ObjectId,
        ref: 'Group'
    },
    projects: [{type: mongoose.Schema.Types.ObjectId, ref: 'Project'}],
});

如果删除了Group/Project,我如何更新我的分配模式.

If a Group/Project is removed, how can i update my Assignment Schema.

var ProjectSchema = new Schema({
    name: String
});

var GroupSchema = new Schema({
    name: String
});

从stackoverflow中的几个答案中,我了解了有关删除中间件的信息,但是我不确定如何为one-one and one-many关系实现它.谁能给我示范一个这样做的例子.

From couple of answers in stackoverflow, i came to know about the remove middleware, but i am not sure how to implement it for one-one and one-many relationship. Can anyone show me an example of doing it.

ProjectSchema.pre('remove', function(next){
    this.model('Assignment').update(

    );
});

推荐答案

关系:

  1. 一个one-to-one is a relationship,使得一个国家只有一个 首都和首都是唯一一个州的首都
  2. 一个one-to-many is a relationship,以至于母亲有很多 孩子,孩子只有一个母亲
  3. many-to-many is a relationship,这样一本书可以由 几个作者或合著者,而一个作者可以写几个 书.
  1. A one-to-one is a relationship such that a state has only one capital city and a capital city is the capital of only one state
  2. A one-to-many is a relationship such that a mother has many children, and the children have only one mother
  3. A many-to-many is a relationship such that a book can be written by several authors or co-authors, while an author can write several books.

一对一关系-如果删除了Project/Group,我如何更新我的Assignment架构.

one-one relationship - If a Project/Group is removed, how can i update my Assignment Schema.

通常,您会将一个project映射到一个assignment,类似地,将一个assignment映射到一个project.您可以在这里执行的操作是删除一个项目,然后在分配模型中找到关联的project并删除其引用.

Typically you will have one project mapped to one assignment and similarly one assignment mapped to one project. what you can do here is removing a project and then find the associated project in assignment model and remove their references.

delete: function(req, res) {
   return Project.findById(req.params.id, function(err, project){
         return project.remove(function(err){
             if(!err) {
                 Assignment.update({_id: project.assignment}}, 
                      {$pull: {projects: project._id}}, 
                          function (err, numberAffected) {
                            console.log(numberAffected);
                      } else {
                        console.log(err);                                      
                    }
                  });
            });
        });
}

一对多关系-如果删除了Project/Group,我如何更新我的Assignment架构.

one-many relationship - If a Project/Group is removed, how can i update my Assignment Schema.

在这种情况下,我们要删除一个项目,然后找到属于该project的所有assignments,并从中删除其引用.在这种情况下,单个项目可能会有很多分配.

In this scenario we are removing a project and then finding all the assignments which belongs to this project and removing its reference from them. Here the situation is, there can be many assignments for a single project.

delete: function(req, res) {
   return Project.findById(req.params.id, function(err, project){
         return project.remove(function(err){
             if(!err) {
                 Assignment.update({_id: {$in: project.assingments}}, 
                      {$pull: {project: project._id}}, 
                           function (err, numberAffected) {
                            console.log(numberAffected);
                      } else {
                        console.log(err);                                      
                    }
                  });
            });
        });
}

删除中间件

您可以通过middleware达到约翰尼(Johnny)指出的相同目的,只是对此的更正.

You could achieve the same thing via middleware as pointed out by Johnny, just a correction on that..

ProjectSchema.pre('remove', function (next) {
    var project = this;
    project.model('Assignment').update(
        { projects: {$in: project.assignments}}, 
        { $pull: { project: project._id } }, 
        { multi: true }, 
        next
     );
});

通常,可以有很多projects属于assignment,很多assignments属于同一个project.在Project模式中将有一个assignment列,其中一个项目将与多个分配相关.

Typically there can be many projects belonging to an assignment and many assignments belonging to the same project. You will have an assignment column in your Project Schema where one project will relate to multiple assignments.

注意:删除中间件不适用于模型,而仅适用于您的文档.如果要使用remove中间件,请确保在删除功能中先找到id的project,然后在返回的document上应用remove方法,以便上面的方法起作用...您的删除功能应如下所示像这样.

Note: remove middleware won't work on models and it would only work on your documents. If you are going with remove middleware ensure in your delete function, you find project by id first and then on the returned document apply the remove method, so for the above to work... your delete function would look like this.

delete: function(req, res) {
   return Project.findById(req.params.id, function(err, project){
         return project.remove(function(err){
             if(!err) {
                  console.log(numberAffected);
             } 
           });                
    });
 }

这篇关于删除一对一和一对多引用-猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆