{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/2283","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/2283/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/2283/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/2283/events","html_url":"https://github.com/Automattic/mongoose/issues/2283","id":42098735,"number":2283,"title":"unable to populate relationship after saving a record without having to load it again","user":{"login":"mgenev","id":3029803,"avatar_url":"https://avatars.githubusercontent.com/u/3029803?v=3","gravatar_id":"","url":"https://api.github.com/users/mgenev","html_url":"https://github.com/mgenev","followers_url":"https://api.github.com/users/mgenev/followers","following_url":"https://api.github.com/users/mgenev/following{/other_user}","gists_url":"https://api.github.com/users/mgenev/gists{/gist_id}","starred_url":"https://api.github.com/users/mgenev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mgenev/subscriptions","organizations_url":"https://api.github.com/users/mgenev/orgs","repos_url":"https://api.github.com/users/mgenev/repos","events_url":"https://api.github.com/users/mgenev/events{/privacy}","received_events_url":"https://api.github.com/users/mgenev/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2014-09-05T22:47:20Z","updated_at":"2016-06-09T12:21:26Z","closed_at":"2016-05-11T03:56:26Z","body":"Whether I'm creating or updating a record of type article, I have to use .save(). Before I hit save, I have the entire user in req.user\r\n```\r\n{ provider: 'local',\r\n  email: 'test1@test.com',\r\n  hashed_password: '49de749649b49c9bdf31073a8975bbc128b09d81',\r\n  salt: '298408390498',\r\n  username: 'test1',\r\n  name: 'test1',\r\n  _id: 540a2e06c09f8f4e922dd773,\r\n  __v: 0 }\r\n```\r\nThen I do:\r\n```\r\nvar article = new Article(req.body.article);\r\n    article.user = req.user;\r\n```\r\n\r\nMongoose only keeps the id part and when I try to get article.user out it just gives me the string 540a2e06c09f8f4e922dd773, the rest is scrapped. Using this id when I save, mongoose accurately saves the reference in mongo\r\n```\r\n article.save(function(err, article) {\r\n                res.jsonp({\r\n                    article: article\r\n                });\r\n    });\r\n```\r\nBut it returns the user as an id in the callback, not as an embedded record. Even if I try after the callback  to attach the whole user object so I can return it to the client from the save callback, everything but id is scrapped. \r\nIf I want to make it embed, I normally call populate, but apparently I don't have that method after saving, only when I'm doing finds. Which means even though I already have the new record which I just saved or updated, I have to go _back_ to the database with a find so I can do a populate. (load is a convenience method I have for find then populate)\r\n\r\n```\r\narticle.save(function(err) {\r\n\r\n        Article.load(article._id, function(err, result) {\r\n\r\n            if (err) {\r\n                return res.send('users/signup', {\r\n                    errors: err.errors,\r\n                    article: result\r\n                });\r\n            } else {\r\n                res.jsonp({\r\n                    article: result\r\n                });\r\n            }\r\n        });\r\n    });\r\n```\r\n\r\nThis is redundant and inefficient, it more than doubles the time needed to perform this operation and I was looking for a proper way to do this, but even the guides here for updating refs tell me to do a redundant find and populate...\r\n\r\nhttp://mongoosejs.com/docs/populate.html\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/224878359","html_url":"https://github.com/Automattic/mongoose/issues/2283#issuecomment-224878359","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/2283","id":224878359,"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":"2016-06-09T12:21:26Z","updated_at":"2016-06-09T12:21:26Z","body":"Not really possible, closest thing is findOneAndUpdate but that has a few limitations. Plus I'm not sure a saveThenFind function would really provide much value"}}
{"actor":{"display_login":"vkarpov15"}}