I have User and Survey model. Survey model looks like this:
const SurveySchema = mongoose.Schema({
title: {
type: String
},
options: [{
value: String,
votes: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]
}]
})
So in UserSchema, I wanna make cascade delete, when User deletes his account, I wanna remove him from votes. So I tried with something like this:
UserSchema.pre('remove', function (next) {
this.model('Survey').update({ options: { $elemMatch: { votes: this._id } } }, { $pull: { votes: this._id } }, {multi: true}).exec();
next();
});
I know this isn't even close to solution, but just so you can see in which way I'm going.