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

How to access the created/updated sub-document? #4194

Closed
mtvs opened this issue Jun 1, 2016 · 6 comments
Closed

How to access the created/updated sub-document? #4194

mtvs opened this issue Jun 1, 2016 · 6 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@mtvs
Copy link

mtvs commented Jun 1, 2016

I noticed when you save a parent document, the save middleware of its sub-document`s schema is called for all of the sub-documents.

Is there a way that you can access the created/updated sub-document?

subDocSchema.post('save'. function (doc) {
    console.log(doc);
}

then

parent.subDocs.push({note: 'new doc'});
parent.save();

results

// {note: 'new doc'}
// {note: 'old doc'}
// ...
@mtvs mtvs changed the title How to access the created/updated sub-document in mongoosejs? How to access the created/updated sub-document? Jun 1, 2016
@vkarpov15
Copy link
Collaborator

I'm sorry, I don't understand the question, can you please clarify? In my mind it looks like you've got the answer to your question in your code samples.

@vkarpov15 vkarpov15 added this to the 4.4.20 milestone Jun 1, 2016
@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Jun 1, 2016
@mtvs
Copy link
Author

mtvs commented Jun 2, 2016

@vkarpov15 Normally when a document is saved the pre/post save middleware of its schema are called with the newly created/updated doc but when a sub-doc is created/updated by changing the document-array inside its parent document and then saving the parent document then the pre/post save middleware of the sub-document schema are called for all the sub-docs not just the newly created/updated one.

So how are you supposed to detect the new one to for example emit an event with that?

@vkarpov15
Copy link
Collaborator

Documents, including subdocs, have an isNew property that tells you whether the doc has been saved to the db or not.

subDocSchema.post('save'. function (doc) {
  if (doc.isNew) {
    console.log('new doc', doc);
  }
});

@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Jun 4, 2016
@vkarpov15 vkarpov15 removed this from the 4.4.20 milestone Jun 4, 2016
@mtvs
Copy link
Author

mtvs commented Jun 5, 2016

@vkarpov15 Inside post save middleware of the sub document schema, isNew is false for all the sub documents.

I think this behavior (execution of save middleware for all sub docs) is something against the expectation and also a development obstacle.

@vkarpov15
Copy link
Collaborator

Yeah because after you save, isNew will always be false. Take a look at #1474 for a workaround.

And IMO it's the correct behavior - save middleware happens for parent docs regardless of whether or not there's anything to save, so makes sense that the child docs get the same behavior too.

@mtvs
Copy link
Author

mtvs commented Jun 7, 2016

@vkarpov15 Ok, thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants