Skip to content
This repository

Remove hook not triggered when removing documents #1241

Closed
SirPepe opened this Issue · 11 comments

7 participants

Peter Kröner Aaron Heckmann Ivan Jason Valdron Ramsey Darksheep42 Valeri Karpov
Peter Kröner

The pre/post hooks for remove are only triggered when using the document instance's remove() method, not when the model's methods are used:

var mongoose = require("mongoose");
var db = mongoose.createConnection('mongodb://localhost/experiment');
db.once('open', function(){

  testSchema = new mongoose.Schema({ title: String });
  testSchema.post('remove', function(removed){
    console.log('removed', removed.title);
  });
  var Test = db.model('Test', testSchema);

  // > "removed A"
  new Test({ title: 'A' }).save(function(err, created){
    created.remove();
  });

  // Nothing :(
  Test({ title: 'B' }).save(function(err, created){
    Test.remove({ title: 'B' }).exec();
  });

  // Nothing :(
  Test({ title: 'C' }).save(function(err, created){
    Test.find({ title: 'C' }).remove();
  });

});
Aaron Heckmann
Collaborator

this works as expected. its the same thing for Model.update. no middleware is called b/c middleware only execute on mongoose documents which are not involved when sending a remove command directly to MongoDB through the use of Model.remove().

I'll leaving this open for now until I fix the Model.remove docs to be inline with Model.update. Thanks for the heads up.

Peter Kröner

I see. Is there any way to hook into all remove operations?

Aaron Heckmann
Collaborator

not presently.

Peter Kröner

Ok. Thank you!

Aaron Heckmann
Collaborator

well you cooould if you monkey patched Model.remove but thats up to you :)

Ivan
intech commented

Not working with findOneAndRemove

Jason Valdron

Also doesn't seem to work when you call Model.remove(_id).exec();

Ramsey

This is how I got around the issue:

Post.findOneAndRemove({'id': data.post, 'user.uid': socket.handshake.user.id}, function(err, post) {
    post.remove();
});

models.js

postSchema.post('remove', function(doc) {
    console.log('removed');
});

// prints 'removed' once

The remove hook is not fired on findOneAndRemove, however, it works when I call remove in the callback

Darksheep42

Ok but Why ? iam sorry i didnt get why the hook is executed on Model.remove() and not on Query.remove(). Now why should i use the hooks if they arent called everytime ?

what's the use ?

Darksheep42

I mean : Should i use the hooks and avoid Query.remove or not use the hooks and always remove manually ?

Valeri Karpov
Collaborator

@Darksheep42 if you want to use hooks, use doc.remove(). The reason why things like MyModel.remove(); doesn't support hooks is that the removed document may not exist in memory, so you would first have to query mongodb for the document and then tell mongodb to remove it for hooks to work properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.