{"action":"created","issue":{"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2645","labels_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2645/labels{/name}","comments_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2645/comments","events_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2645/events","html_url":"https://github.com/LearnBoost/mongoose/issues/2645","id":56462185,"number":2645,"title":"pre-save in nested documents not executed correctly (???)","user":{"login":"Azuri","id":1536918,"avatar_url":"https://avatars.githubusercontent.com/u/1536918?v=3","gravatar_id":"","url":"https://api.github.com/users/Azuri","html_url":"https://github.com/Azuri","followers_url":"https://api.github.com/users/Azuri/followers","following_url":"https://api.github.com/users/Azuri/following{/other_user}","gists_url":"https://api.github.com/users/Azuri/gists{/gist_id}","starred_url":"https://api.github.com/users/Azuri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Azuri/subscriptions","organizations_url":"https://api.github.com/users/Azuri/orgs","repos_url":"https://api.github.com/users/Azuri/repos","events_url":"https://api.github.com/users/Azuri/events{/privacy}","received_events_url":"https://api.github.com/users/Azuri/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-04T00:31:45Z","updated_at":"2015-02-04T18:36:14Z","closed_at":null,"body":"I have the following code:\r\n``` js\r\nvar ChildSchema = mongoose.Schema({\r\n    name: String\r\n});\r\nChildSchema.pre('save', function (next) {\r\n    console.log('child pre-save -', this.name);\r\n    next();\r\n});\r\nvar Child = mongoose.model('Child', ChildSchema);\r\n\r\nvar ParentSchema = mongoose.Schema({\r\n    children: [Child.schema]\r\n});\r\nParentSchema.pre('save', function (next) {\r\n    console.log('parent pre-save');\r\n    this.children = [];\r\n    this.children.push({ name: 'jane' });\r\n    this.children.push({ name: 'john' });\r\n        \r\n    next();\r\n});\r\nvar Parent = mongoose.model('Parent', ParentSchema);\r\n\r\nvar p = new Parent({ children: [{ name: 'mike' }, { name: 'bob' }, { name: 'susan' }] });\r\n\r\np.save(function (err) { console.log(p); });\r\n```\r\n\r\nResult\r\n```\r\nparent pre-save\r\nchild pre-save - mike\r\nchild pre-save - bob\r\nchild pre-save - susan\r\n{ __v: 0,\r\n  _id: 54d16604e4f4beb422e39442,\r\n  children:\r\n   [ { name: 'jane', _id: 54d16604e4f4beb422e39446 },\r\n     { name: 'john', _id: 54d16604e4f4beb422e39447 } ] }\r\n```\r\n\r\nIs this normal behavior? How can I get around this? I do think that I should get pre-save hooks for `john` and `jane`"},"comment":{"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/comments/72911434","html_url":"https://github.com/LearnBoost/mongoose/issues/2645#issuecomment-72911434","issue_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2645","id":72911434,"user":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"created_at":"2015-02-04T18:36:14Z","updated_at":"2015-02-04T18:36:14Z","body":"Yeah this is a limitation in mongoose 3.8.x: the way it handles pre hooks on child docs is a little strange. This issue is fixed in 4.0.0-rc1, I get the following output using that version:\r\n\r\n```\r\nparent pre-save\r\nchild pre-save - jane\r\nchild pre-save - john\r\n{ __v: 0,\r\n  _id: 54d266132187101a14e0b71e,\r\n  children: \r\n   [ { name: 'jane', _id: 54d266132187101a14e0b722 },\r\n     { name: 'john', _id: 54d266132187101a14e0b723 } ] }\r\n```\r\n\r\nI'll investigate fixing this in 3.8.x, but if you want a quick fix try upgrading to 4.0.0-rc1 :)"}}