Skip to content

Commit

Permalink
docs(document): explain that $isNew is false in post('save') hooks
Browse files Browse the repository at this point in the history
Fix #11990
  • Loading branch information
vkarpov15 committed Nov 12, 2022
1 parent d98b285 commit 87d843d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/document.js
Expand Up @@ -219,6 +219,17 @@ function Document(obj, fields, skipId, options) {
*
* const user = await User.findOne({ name: 'John Smith' });
* user.$isNew; // false
*
* Mongoose sets `$isNew` to `false` immediately after `save()` succeeds.
* That means Mongoose sets `$isNew` to false **before** `post('save')` hooks run.
* In `post('save')` hooks, `$isNew` will be `false` if `save()` succeeded.
*
* #### Example:
*
* userSchema.post('save', function() {
* this.$isNew; // false
* });
* await User.create({ name: 'John Smith' });
*
* For subdocuments, `$isNew` is true if either the parent has `$isNew` set,
* or if you create a new subdocument.
Expand Down

0 comments on commit 87d843d

Please sign in to comment.