{"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://avatars0.githubusercontent.com/u/4144726?v=4","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":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-08-05T10:53:28Z","updated_at":"2018-05-29T21:47:32Z","closed_at":"2016-08-06T20:28:01Z","author_association":"NONE","body":"Hi guys,\n\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.\n\n``` javascript\n// Populate\nconst User = new Schema({\n  name: String,\n  articles: [{ type: Schema.Types.ObjectId, ref: 'Article' }]\n});\n\nconst Article = new Schema({\n  title: String,\n  authors: [{ type: Schema.Types.ObjectId, ref: 'User' }]\n});\n\nconst article = new Articles({ title: 'My post', authors: ['1234', '5678'] }); \narticle.save((err, article) => {...});\n\nArticle.find({}).populate('authors') .exec((err, articles) => {...}); // Work fine, with populated values\n\nUser.find({}).populate('articles') .exec((err, articles) => {...});  // Normal behavior, the articles attributes is an empty array.\n```\n\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.\n\n``` javascript\n// Populate + virtuals\nconst User = new Schema({\n  name: String,\n  articles: [{ type: Schema.Types.ObjectId, ref: 'Article' }]\n});\n\nconst Article = new Schema({\n  title: String,\n  authors: [{ type: Schema.Types.ObjectId, ref: 'User' }]\n});\n\n// Try to also apply virtual on existing relationships attributes\nUser.virtual('articles', {\n  ref: 'Article',\n  localField: '_id',\n  foreignField: 'authors'\n});\n\nArticle.virtual('authors', {\n  ref: 'User',\n  localField: '_id',\n  foreignField: 'articles'\n});\n\nconst user1 = new User({ name: 'John Doe' }) // _id = 1234\nconst user2 = new User({ name: 'Mickey Mouse' }) // _id = 5678 \n\nconst article = new Articles({ title: 'My post', authors: ['1234', '5678'] }); \narticle.save((err, article) => {...});\n\nArticle.find({}).populate('authors') .exec((err, articles) => {...}); // Work fine, with populated values\n\nUser.find({}).populate('articles') .exec((err, articles) => {...}); // I would also get the articles thanks to the virtual population\n\n```\n\nAny solution to do that?\n\nThanks :)\n"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/392956685","html_url":"https://github.com/Automattic/mongoose/issues/4393#issuecomment-392956685","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/4393","id":392956685,"user":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars2.githubusercontent.com/u/1620265?v=4","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":"2018-05-29T21:47:32Z","updated_at":"2018-05-29T21:47:32Z","author_association":"COLLABORATOR","body":"@patil16nit are you actually waiting for `userA.save()`, `userB.save()`, and `newArticle.save()` to finish before executing the queries? That might explain the issue, you need to wait for the documents to actually be saved before querying."}}
{"actor":{"display_login":"vkarpov15"}}