删除Mongoose中的多对多引用

时间:2021-09-13 18:39:28

One of my mongoose schemas is a many to many relationship:

我的一个mongoose模式是多对多的关系:

var UserSchema = new Schema({
   name       : String,
   groups  : [ {type : mongoose.Schema.ObjectId, ref : 'Group'} ]
});

var GroupSchema = new Schema({
   name       : String,
   users  : [ {type : mongoose.Schema.ObjectId, ref : 'User'} ]
});

If I remove a group, is there anyway to remove that group objectId from all the user's 'groups' array?

如果我删除一个组,无论如何要从所有用户的'groups'数组中删除该组objectId?

GroupSchema.pre('remove', function(next){
    //Remove group._id from all the users
})

2 个解决方案

#1


15  

You're on the right track to use 'remove' middleware for this. In the middleware function, this is the group instance being removed and you can access the other models via its model method. So you can do something like:

您正在使用“删除”中间件进行此操作。在中间件功能中,这是要删除的组实例,您可以通过其模型方法访问其他模型。所以你可以这样做:

GroupSchema.pre('remove', function(next){
    this.model('User').update(
        {_id: {$in: this.users}}, 
        {$pull: {groups: this._id}}, 
        {multi: true},
        next
    );
});

Or if you want to support cases where the users field in your group instance may not be complete you could do:

或者,如果您希望支持组实例中的用户字段可能不完整的情况,您可以执行以下操作:

GroupSchema.pre('remove', function(next){
    this.model('User').update(
        {groups: this._id}, 
        {$pull: {groups: this._id}}, 
        {multi: true},
        next
    );
});

But as WiredPrairie notes, for this option you'd want an index on groups for good performance.

但正如WiredPrairie指出的那样,对于这个选项,你需要一个关于组的索引以获得良好的性能。

#2


0  

I use my patched version of mongoose-relationship "plugin" to solve this: take a look on https://github.com/begrossi/mongoose-relationship/tree/remove-from-parent-if-removed-from-child-set.

我使用我的修补版本的mongoose-relationship“插件”来解决这个问题:看看https://github.com/begrossi/mongoose-relationship/tree/remove-from-parent-if-removed-from-child-组。

Bruno Grossi

布鲁诺·格罗西

#1


15  

You're on the right track to use 'remove' middleware for this. In the middleware function, this is the group instance being removed and you can access the other models via its model method. So you can do something like:

您正在使用“删除”中间件进行此操作。在中间件功能中,这是要删除的组实例,您可以通过其模型方法访问其他模型。所以你可以这样做:

GroupSchema.pre('remove', function(next){
    this.model('User').update(
        {_id: {$in: this.users}}, 
        {$pull: {groups: this._id}}, 
        {multi: true},
        next
    );
});

Or if you want to support cases where the users field in your group instance may not be complete you could do:

或者,如果您希望支持组实例中的用户字段可能不完整的情况,您可以执行以下操作:

GroupSchema.pre('remove', function(next){
    this.model('User').update(
        {groups: this._id}, 
        {$pull: {groups: this._id}}, 
        {multi: true},
        next
    );
});

But as WiredPrairie notes, for this option you'd want an index on groups for good performance.

但正如WiredPrairie指出的那样,对于这个选项,你需要一个关于组的索引以获得良好的性能。

#2


0  

I use my patched version of mongoose-relationship "plugin" to solve this: take a look on https://github.com/begrossi/mongoose-relationship/tree/remove-from-parent-if-removed-from-child-set.

我使用我的修补版本的mongoose-relationship“插件”来解决这个问题:看看https://github.com/begrossi/mongoose-relationship/tree/remove-from-parent-if-removed-from-child-组。

Bruno Grossi

布鲁诺·格罗西