For pre('save')
middleware there is isNew/isModified to figure out if the document is new. I couldn't find something similar for post('save')
. I am wondering is there something similar for post('save')
?
See #1474 and http://stackoverflow.com/questions/17099025/determine-whether-doc-is-new-or-exists-in-mongoose-post-middlewares-save . Long story short, you can do this yourself pretty easily by recording the value of isNew
in pre('save')
. The isNew
field is something that Mongoose uses internally to keep track of 'has this document been persisted to the database yet?', so Mongoose sets it to false after save.
This is pretty insane. Can't understand why not to put this logic inside mongoose.
I agree with @ericsaboia
As far as I understand it, this is trickier than its worth. Post save hooks are run after the save function, so there's no way to clean up internal state like isNew
after post hooks run, unless we either add an extra "run this post hook after all other post hooks" logic, which is very janky IMO. I'm open to ideas on how this would work, but I don't have a good solution just yet.
Most helpful comment
This is pretty insane. Can't understand why not to put this logic inside mongoose.