One of my mongoose schemas is a many to many relationship:
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?
GroupSchema.pre('remove', function(next){
//Remove group._id from all the users
})
See Question&Answers more detail:os