Mongoose: isNew or isModified() for post hooks

Created on 25 Jun 2014  路  4Comments  路  Source: Automattic/mongoose

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')?

Most helpful comment

This is pretty insane. Can't understand why not to put this logic inside mongoose.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukasz-zak picture lukasz-zak  路  3Comments

simonxca picture simonxca  路  3Comments

ArThoX picture ArThoX  路  3Comments

efkan picture efkan  路  3Comments

varunjayaraman picture varunjayaraman  路  3Comments