Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

javascript - Removing one-one and one-many references - Mongoose

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

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

Below is my Asssignment Schema

var AssignmentSchema = new Schema({
    name: String,
    group: {
        type: Schema.Types.ObjectId,
        ref: 'Group'
    },
    projects: [{type: mongoose.Schema.Types.ObjectId, ref: '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
});

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(

    );
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Relationships:

  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.

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

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);                                      
                    }
                  });
            });
        });
}

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

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);                                      
                    }
                  });
            });
        });
}

Remove middleware

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
     );
});

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.

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);
             } 
           });                
    });
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...