pre, post middleware are not executed on findByIdAndUpdate
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.
by design. there are docs involved to call hooks on.
correction, there are no docs to call hooks on.
So? If I want to call pre, post middleware I need to use the first approach?
yes that is correct. Model.update,findByIdAndUpdate,findOneAndUpdate,findOneAndRemove,findByIdAndRemove are all commands executed directly in the database.
This should definitely be clarified in the guide, especially if you're talking about validation in that same page and describing findByIdAndUpdate as "better"
if you click on the link "better" it takes you to the full documentation of the method where it explains validation etc.
feel free to send a pull request to add something you feel is better. its pretty easy to do so: https://github.com/LearnBoost/mongoose/blob/master/CONTRIBUTING.md
On Wed, Oct 31, 2012 at 6:03 PM, Jesse Fulton [email protected]:
This should definitely be clarified in the guidehttp://mongoosejs.com/docs/documents.html, especially if you're talking about validation in that same page and describing findByIdAndUpdate as "better"
— Reply to this email directly or view it on GitHubhttps://github.com/LearnBoost/mongoose/issues/964#issuecomment-9967865.
Aaron @aaronheckmann https://twitter.com/#!/aaronheckmann
Added notes to the middleware doc
Pull request: https://github.com/LearnBoost/mongoose/pull/1750
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
@albanm certain methods bypass mongoose completely, so you don't get the middleware hooks. AFAIK, the only way to get the hooks to execute is to use separate find() and save() calls as mentioned above.
I get that and it makes sense. Pre middlewares are out of question as there is no fetching prior to certain methods. But still, mongoose should be able to wrap the update operations that also return the updated documents and trigger some post hooks.
I think @albanm is right, People may want the same functionality when they use the same function. How about wrap those 'directly update' methods with some interception of check that if there's any hooks exist? If hook exist, use it, or call original update methods otherwise.
+1
+1
+1
+1
:+1:
+1
:-1:
I get the feature request, but the longer I work with middleware, I find myself needing to bypass middleware often when calling db update scripts and other items that are not routine to my normal operations. By using static methods it becomes very easy to create a custom wrapper that implements the above feature requests already. Since mongoose is now opening to ideas for version 4, it is unlikely that any API change of this magnitude will happen in v3. I request that this be moved to v4 discussion.
I would however, :+1: if I was able to disable middleware on a save call. I often find myself with a mongoose object that was passed from another function and simply want to perform a .save - in this situation, doing a .save is preferable to writing a new query. If this is possible, please point it out
Either way, amazing library. Kudos to awesome maintainers. Not sure how I would operate without it.
+1
A simple way to add hooks for these method is overwrite the embed functions:
_update = UserModel.update
UserModel.update = (conditions, update) ->
update.updatedAt or= new Date
_update.apply this, arguments
Then every update call of mongoose will fix the updatedAt key of data.
You may give a try of this model limbo. It is a simple wrapper of mongoose model, supporting bind static/method/overwrite to all the schemas, and call rpc methods to query the mongodb.