删除一对一和一对多的引用 - Mongoose [英] Removing one-one and one-many references - Mongoose
问题描述
我有一个 Assignment schema
,它引用了 Groups
和 Projects
.
I have an Assignment schema
which has references to Groups
and Projects
.
Assignment == Group [One-One Relationship]
Assignment == Projects [One-Many Relationship]
下面是我的Assignment
Schema
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'}],
});
如果删除了 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 中的几个答案,我开始了解 remove 中间件,但我不确定如何为 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(
);
});
推荐答案
关系:
one-to-one 是一种关系
使得一个状态只有一个首都和首都是唯一一个州的首都一对多是一种关系
,这样一个母亲有很多孩子们,孩子们只有一个妈妈many-to-many 是一种关系
使得一本书可以由几个作者或共同作者,而一个作者可以写几个书.
- 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 - A
one-to-many is a relationship
such that a mother has many children, and the children have only one mother - 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);
}
});
});
}
这篇关于删除一对一和一对多的引用 - Mongoose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!