Skip to content

Commit

Permalink
Merge pull request #12685 from Automattic/vkarpov15/gh-11990
Browse files Browse the repository at this point in the history
docs(document): explain that `$isNew` is `false` in post('save') hooks
  • Loading branch information
vkarpov15 authored Nov 13, 2022
2 parents 0c1ac69 + 349e038 commit c77dd1b
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,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 c77dd1b

Please sign in to comment.