{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3575","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3575/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3575/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3575/events","html_url":"https://github.com/Automattic/mongoose/issues/3575","id":116953867,"number":3575,"title":"When adding pre-populated objects onto a model instance array property, they get converted to ObjectID's","user":{"login":"adamreisnz","id":490562,"avatar_url":"https://avatars.githubusercontent.com/u/490562?v=3","gravatar_id":"","url":"https://api.github.com/users/adamreisnz","html_url":"https://github.com/adamreisnz","followers_url":"https://api.github.com/users/adamreisnz/followers","following_url":"https://api.github.com/users/adamreisnz/following{/other_user}","gists_url":"https://api.github.com/users/adamreisnz/gists{/gist_id}","starred_url":"https://api.github.com/users/adamreisnz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamreisnz/subscriptions","organizations_url":"https://api.github.com/users/adamreisnz/orgs","repos_url":"https://api.github.com/users/adamreisnz/repos","events_url":"https://api.github.com/users/adamreisnz/events{/privacy}","received_events_url":"https://api.github.com/users/adamreisnz/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/71","html_url":"https://github.com/Automattic/mongoose/milestone/71","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/71/labels","id":1402965,"number":71,"title":"4.2.6","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":0,"closed_issues":9,"state":"closed","created_at":"2015-11-09T20:11:04Z","updated_at":"2015-11-17T14:54:49Z","due_on":"2015-11-16T08:00:00Z","closed_at":"2015-11-17T14:54:49Z"},"comments":13,"created_at":"2015-11-14T21:44:46Z","updated_at":"2016-12-21T00:30:21Z","closed_at":"2015-11-16T17:17:59Z","body":"I've noticed what seems to be a discrepancy in the way Mongoose handles pre-populated values.\nConsider the following simple example schema:\n\n``` javascript\nvar FooSchema = new Schema({\n  bar: {\n    type: Schema.Types.ObjectId,\n    ref: 'Bar'\n  },\n  bars: [{\n    type: Schema.Types.ObjectId,\n    ref: 'Bar'\n  }]\n});\n```\n\nNow  suppose we get some Bar objects and we create a new Foo:\n\n``` javascript\n//Assume these variables are Bar model instances with the following properties:\nvar bar1 = {id: 1, name: 'BarOne'};\nvar bar2 = {id: 2, name: 'BarTwo'};\nvar bar3 = {id: 3, name: 'BarThree'};\n\n//Prepare data for a new Foo instance:\nvar fooData = {\n  bar: bar1,\n  bars: [bar2, bar3]\n};\n\n//Create new Foo instance\nFoo.create(fooData).then(function(foo) {\n  //...\n});\n```\n\nAfter creation, the foo instance looks like this:\n\n``` javascript\n{\n  bar: {\n    id: 1,\n    name: 'BarOne'\n  },\n  bars: [2, 3]\n}\n```\n\nWhat has happened, is that Mongoose recognised that `bar1` was a `Bar` instance, so it left it on the `foo` instance as a populated property. However, it has un-populated the array of `bars` and flattened them to just ID's, even though they were also pre-populated.\n\nWhy the inconsistency?\n\nI would expect the foo instance to either un-populate all properties, or keep them all populated, if the data is available, e.g.:\n\n``` javascript\n{\n  bar: 1,\n  bars: [2, 3]\n}\n```\n\nor\n\n``` javascript\n{\n  bar: {\n    id: 1,\n    name: 'BarOne'\n  },\n  bars: [{\n    id: 2,\n    name: 'BarTwo'\n  }, {\n    id: 3,\n    name: 'BarThree'\n  }]\n}\n```\n\nAny way I can force this behaviour one way or another for both object refs and arrays of object refs?\n"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/268399933","html_url":"https://github.com/Automattic/mongoose/issues/3575#issuecomment-268399933","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3575","id":268399933,"user":{"login":"emmanuelbuah","id":432664,"avatar_url":"https://avatars.githubusercontent.com/u/432664?v=3","gravatar_id":"","url":"https://api.github.com/users/emmanuelbuah","html_url":"https://github.com/emmanuelbuah","followers_url":"https://api.github.com/users/emmanuelbuah/followers","following_url":"https://api.github.com/users/emmanuelbuah/following{/other_user}","gists_url":"https://api.github.com/users/emmanuelbuah/gists{/gist_id}","starred_url":"https://api.github.com/users/emmanuelbuah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emmanuelbuah/subscriptions","organizations_url":"https://api.github.com/users/emmanuelbuah/orgs","repos_url":"https://api.github.com/users/emmanuelbuah/repos","events_url":"https://api.github.com/users/emmanuelbuah/events{/privacy}","received_events_url":"https://api.github.com/users/emmanuelbuah/received_events","type":"User","site_admin":false},"created_at":"2016-12-21T00:30:21Z","updated_at":"2016-12-21T00:30:21Z","body":"This doesn't seem to work if the array ref schema is the base type using discriminator. \r\n\r\n`\r\nvar shapeSchema = new mongoose.Schema({discriminatorKey: 'kind'});\r\n\r\nvar Shape = mongoose.model('Shape', shapeSchema);\r\n\r\nvar Circle = Shape.discriminator('Circle', new mongoose.Schema({\r\n radius: { type: Number }\r\n},{discriminatorKey: 'kind'}))\r\n\r\n\r\nvar Square = Shape.discriminator('Square', new mongoose.Schema({\r\n sides: { type: Number }\r\n},{discriminatorKey: 'kind'}))\r\n\r\nvar FooSchema = new Schema({\r\n  bar: {\r\n    type: Schema.Types.ObjectId,\r\n    ref: 'Shape'\r\n  },\r\n  bars: [{\r\n    type: Schema.Types.ObjectId,\r\n    ref: 'Shape'\r\n  }]\r\n});\r\n\r\nvar Foo = mongoose.model('Foo', fooSchema);\r\n\r\nvar test = new Foo({ });\r\ntest.bars = [{ kind: Cirle , _id: ObjectId(..) } , { kind: Cirle , _id: ObjectId(..) } ];\r\n//test.bar[0] is objectid rather than circle object\r\ntest.save()\r\n//test.bar[0] is objectid rather than circle object\r\n\r\n`"}}
{"actor":{"display_login":"emmanuelbuah"}}