-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
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 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? |
Documents, including subdocs, have an subDocSchema.post('save'. function (doc) {
if (doc.isNew) {
console.log('new doc', doc);
}
}); |
@vkarpov15 Inside post save middleware of the sub document schema, I think this behavior (execution of save middleware for all sub docs) is something against the expectation and also a development obstacle. |
Yeah because after you save, 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. |
@vkarpov15 Ok, thanks for your help. |
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?
then
results
The text was updated successfully, but these errors were encountered: