question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pre, post middleware are not executed on findByIdAndUpdate

See original GitHub issue

Because a findAndUpdate method is presented to cut down the following code:

 Model.findById(_id, function (err, doc) {
      if (doc) {
          doc.field = 'value';
          doc.save(function (err) {
                 // do something;
          });
      }
 });

to this:

Model
   .findByIdAndUpdate(_id, {$set: {field: 'value'}}, function (err, doc) {
        // do something 
    });

We need to use pre, post middleware exactly the same. At now pre, post middleware are not executed when I make findByIdAndUpdate.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:14
  • Comments:102 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
albanmcommented, Jun 10, 2014

Hello,

I know the issue is closed, but I am not entirely satisfied with the answer. Shouldn’t there be at least a post-save or post-update middleware for findOneAndUpdate and other similar operations ? Is seems ok since the document is returned. Not feasible for pre middlewares or for Model.update, i agree.

It would greatly improve the capabilities of plugins such as mongoosastic that is currently blind to some operations it should be able to support.

If no middleware, does someone have an idea on how to manage some post update operations in a plugin ?

Thanks

22reactions
joerxcommented, Dec 30, 2014

While I sort of understand the reasoning, the lack of hooks on atomic updates IMHO makes Mongoose somewhat pointless in practice. When I use atomic updates any validations, defaults, etc. are not executed, so the entire purpose of using an ODM is defeated. Using find/save will do the job, but is there any guarantee this is always used?

Moreover, usually I would try to avoid find/save since it’s not an atomic operation. MongoDB compensates it’s lack of transaction support by providing powerful atomic query & update features. So I would use these atomic operations but w/o middleware support Mongoose won’t provide much value over the native MongoClient.

Even the examples in http://aaronheckmann.tumblr.com/post/48943525537/mongoose-v3-part-1-versioning would use update, hence bypass middleware. I can use versioning properly or middleware but not combine both? Really, where’s the point in having it?

I don’t even fully understand the technical reasons: If update & co. wrap around the database operations, why can’t we intercept the call and pass the query objects so we can do some validation/customization before we actually do the update?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node js and mongo , pre, post hook middleware are not ...
From official documentation: This function triggers the following middleware. findOneAndUpdate(). So, it won't trigger pre('save') ...
Read more >
[Solved]-Node js and mongo , pre, post hook middleware are ...
Coding example for the question Node js and mongo , pre, post hook middleware are not executed on findByIdAndUpdate-mongodb.
Read more >
Node Js And Mongo, Pre, Post Hook Middleware Are Not ...
pre and post are not called for update operations executed directly on the database including Model.update.findByIdAndUpdate.findOneAndUpdate. However when I ...
Read more >
Mongoose v6.8.1: Middleware
Pre and post save() hooks are not executed on update() , findOneAndUpdate() , etc. You can see a more detailed discussion why in...
Read more >
Middleware pre('remove', ...) not getting called - Google Groups
Because no Mongoose documents are involved, no middleware (hooks) are executed. Does that also apply to Model.findByIdAndRemove? I would like to have the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found