Skip to content
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

When and under what conditions Document.prototype.isNew returns true? #11990

Closed
2 tasks done
haosmos opened this issue Jun 26, 2022 · 5 comments · Fixed by #12685
Closed
2 tasks done

When and under what conditions Document.prototype.isNew returns true? #11990

haosmos opened this issue Jun 26, 2022 · 5 comments · Fixed by #12685
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@haosmos
Copy link

haosmos commented Jun 26, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

First of all, thank you for your wonderful tool - mongoose!

What do you mean, "a new" document? Or, in other words, when and under what conditions a document is considered "new" and when it is not?

The documentation doesn't say anything about this.

chrome_1amBozH4jw

@hasezoey
Copy link
Collaborator

hasezoey commented Jun 27, 2022

From what i know, the document is considered isNew = true if it has been created but not yet saved to the database and has not been retrieved from the database initially:

// the following has "isNew = true"
const newDocNew = new SomeModel();

// after this function it has "isNew = false"
await newDocNew.save();

// the following has "isNew = true" only for some parts inside the function itself, the document returned should be "isNew = false"
const newDocCreate = await SomeMode.create();

// the following has "isNew = false"
const foundDoc = await SomeModel.findOne();

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Jun 27, 2022
@haosmos
Copy link
Author

haosmos commented Jun 28, 2022

From what i know, the document is considered isNew = true if it has been created but not yet saved to the database and has not been retrieved from the database initially:

// the following has "isNew = true"
const newDocNew = new SomeModel();

// after this function it has "isNew = false"
await newDocNew.save();

// the following has "isNew = true" only for some parts inside the function itself, the document returned should be "isNew = false"
const newDocCreate = await SomeMode.create();

// the following has "isNew = false"
const foundDoc = await SomeModel.findOne();

got it. Thank you! ✌️

@haosmos haosmos closed this as completed Jun 28, 2022
@vkarpov15 vkarpov15 reopened this Jul 1, 2022
@vkarpov15 vkarpov15 added this to the 6.4.4 milestone Jul 1, 2022
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Jul 1, 2022
@vkarpov15
Copy link
Collaborator

We should improve the docs on this. The docs for isNew saying that this flag "specifies whether the document is new" isn't very helpful.

@hubab00ba
Copy link

@vkarpov15 What do you think about adding this information to the flag docs for the cases where it's used in 'post' hook? It's already in the docs but for 'Document.prototype.$locals'

schema.pre('save', function() {
  console.log(doc.isNew) // true
  // Mongoose will set `isNew` to `false` if `save()` succeeds
});

schema.post('save', function(doc) {
  console.log(doc.isNew); // false
});

tldr I forgot about that and spent 1 hour to debug why the flag is with value 'false' on insert in 'post' hook

@vkarpov15 vkarpov15 reopened this Oct 24, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.4.4, 6.6.9 Oct 24, 2022
@vkarpov15
Copy link
Collaborator

We'll add a note in the docs that $isNew will be false in post('save') middleware

vkarpov15 added a commit that referenced this issue Nov 13, 2022
docs(document): explain that `$isNew` is `false` in post('save') hooks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants