{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/4393","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/4393/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/4393/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/4393/events","html_url":"https://github.com/Automattic/mongoose/issues/4393","id":169582214,"number":4393,"title":"Bi-directional Many-to-many relationships using virtuals","user":{"login":"Aurelsicoko","id":4144726,"avatar_url":"https://avatars.githubusercontent.com/u/4144726?v=3","gravatar_id":"","url":"https://api.github.com/users/Aurelsicoko","html_url":"https://github.com/Aurelsicoko","followers_url":"https://api.github.com/users/Aurelsicoko/followers","following_url":"https://api.github.com/users/Aurelsicoko/following{/other_user}","gists_url":"https://api.github.com/users/Aurelsicoko/gists{/gist_id}","starred_url":"https://api.github.com/users/Aurelsicoko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Aurelsicoko/subscriptions","organizations_url":"https://api.github.com/users/Aurelsicoko/orgs","repos_url":"https://api.github.com/users/Aurelsicoko/repos","events_url":"https://api.github.com/users/Aurelsicoko/events{/privacy}","received_events_url":"https://api.github.com/users/Aurelsicoko/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Automattic/mongoose/labels/priority","name":"priority","color":"02e10c"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/118","html_url":"https://github.com/Automattic/mongoose/milestones/4.5.9","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/118/labels","id":1883905,"number":118,"title":"4.5.9","description":"","creator":{"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},"open_issues":14,"closed_issues":3,"state":"open","created_at":"2016-07-14T17:26:01Z","updated_at":"2016-08-06T19:20:58Z","due_on":"2016-08-13T07:00:00Z","closed_at":null},"comments":1,"created_at":"2016-08-05T10:53:28Z","updated_at":"2016-08-06T19:21:41Z","closed_at":null,"body":"Hi guys,\r\n\r\nThanks for adding the virtual population, it's saved a lot of time! I still have an issue with the many-to-many. I want to be able to use \"classics relationships\" and virtuals as well.\r\n\r\n```javascript\r\n// Populate\r\nconst User = new Schema({\r\n  name: String,\r\n  articles: [{ type: Schema.Types.ObjectId, ref: 'Article' }]\r\n});\r\n\r\nconst Article = new Schema({\r\n  title: String,\r\n  authors: [{ type: Schema.Types.ObjectId, ref: 'User' }]\r\n});\r\n\r\nconst article = new Articles({ title: 'My post', authors: ['1234', '5678'] }); \r\narticle.save((err, article) => {...});\r\n\r\nArticle.find({}).populate('authors') .exec((err, articles) => {...}); // Work fine, with populated values\r\n\r\nUser.find({}).populate('articles') .exec((err, articles) => {...});  // Normal behavior, the articles attributes is an empty array.\r\n```\r\n\r\nWith the new virtual population, I was trying to make something to not have to maintain the consistency between my many-to-many relationships. I would be able to create an article or user and spent some user's ID or article's ID when I create them and didn't have to think about the relationships direction. I tried this below but it does not work.\r\n\r\n```javascript\r\n// Populate + virtuals\r\nconst User = new Schema({\r\n  name: String,\r\n  articles: [{ type: Schema.Types.ObjectId, ref: 'Article' }]\r\n});\r\n\r\nconst Article = new Schema({\r\n  title: String,\r\n  authors: [{ type: Schema.Types.ObjectId, ref: 'User' }]\r\n});\r\n\r\n// Try to also apply virtual on existing relationships attributes\r\nUser.virtual('articles', {\r\n  ref: 'Article',\r\n  localField: '_id',\r\n  foreignField: 'authors'\r\n});\r\n\r\nArticle.virtual('authors', {\r\n  ref: 'User',\r\n  localField: '_id',\r\n  foreignField: 'articles'\r\n});\r\n\r\nconst user1 = new User({ name: 'John Doe' }) // _id = 1234\r\nconst user2 = new User({ name: 'Mickey Mouse' }) // _id = 5678 \r\n\r\nconst article = new Articles({ title: 'My post', authors: ['1234', '5678'] }); \r\narticle.save((err, article) => {...});\r\n\r\nArticle.find({}).populate('authors') .exec((err, articles) => {...}); // Work fine, with populated values\r\n\r\nUser.find({}).populate('articles') .exec((err, articles) => {...}); // I would also get the articles thanks to the virtual population\r\n\r\n```\r\n\r\nAny solution to do that?\r\n\r\nThanks :)"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/238044158","html_url":"https://github.com/Automattic/mongoose/issues/4393#issuecomment-238044158","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/4393","id":238044158,"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-08-06T19:21:41Z","updated_at":"2016-08-06T19:21:41Z","body":"That code should work as written, I'll try it and see what's wrong later"}}
{"actor":{"display_login":"vkarpov15"}}