I am executing the following code:
User.findByIdAndRemove(id, function(err, user) {
if (err) {
console.log('could not delete user: ' + id);
}
callback(err, user);
});
and it is not executing my pre remove.
UserSchema.pre('remove', function(next) {
var user = this;
Group.removeUserFromGroups(user, function(err, number) {
next();
});
});
In docs : Model.remove
Note:
This method sends a remove command directly to MongoDB, no Mongoose
documents are involved. Because no Mongoose documents are involved, no middleware (hooks) are executed.
Does that also apply to Model.findByIdAndRemove?
I would like to have the find and remove execute in one call to mongo. I.E. I don't really want to do
User.findById(id, function(err, user) {
user.remove();
}
Is that my only option?