New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
schema.pre('save') not triggered by MyModel.update() #419
Comments
This is not a bug. pre save is a hook that fires on instances when their save method is called, not on the model when update is called. Model.update let's you send atomic $set operations directly to mongodb. |
This is a feature I'd like to have; especially helpful when upserting sparse data. |
@aheckman I agree with @martindale, I feel that in UI's where we have |
perhaps |
I have noticed this as well so I assume this is not considered an enhancement worth adding. I decided to use aheckmann's approach where I need to update my object's Setting a date in my case is very sensitive to the instant the save commits to mongodb since I have a background process loading updated docs based on a moving date & time window. I'm not saying there would be a lot of time drift but aheckmann's approach vs. I would love to see some middleware option here. |
So what is the workaround? I expected pre('save') would have caught updates, which semantically seem like saves to me. Since updates are so common, it makes the save hook much less valuable. |
I am not sure if anyone is still watching this or not, but looking into the Mongoose documentations - |
This is fucking bullshit. we can't do anything async in setters. idiots. |
this is a really old thread but ok. There are pre's that actually inturup this or it has been figured out since the post. |
It's all good trent, i went ahead and reorganized my code to make a findById and then a subsequent save, so the pre save hook to salt a password still works. I guess it is what it is, though it would be nice to have a pre update hook as well, it still will take two queries to service the operation since you must read data and check if it has changed any way with isModifed |
Tagging #964 and bumping for interest. |
example.js:
Output:
The pre is triggered by the initial save, but not the update.
The text was updated successfully, but these errors were encountered: