{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3575","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":"adambuczynski","id":490562,"avatar_url":"https://avatars.githubusercontent.com/u/490562?v=3","gravatar_id":"","url":"https://api.github.com/users/adambuczynski","html_url":"https://github.com/adambuczynski","followers_url":"https://api.github.com/users/adambuczynski/followers","following_url":"https://api.github.com/users/adambuczynski/following{/other_user}","gists_url":"https://api.github.com/users/adambuczynski/gists{/gist_id}","starred_url":"https://api.github.com/users/adambuczynski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adambuczynski/subscriptions","organizations_url":"https://api.github.com/users/adambuczynski/orgs","repos_url":"https://api.github.com/users/adambuczynski/repos","events_url":"https://api.github.com/users/adambuczynski/events{/privacy}","received_events_url":"https://api.github.com/users/adambuczynski/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-14T21:44:46Z","updated_at":"2015-11-16T17:19:43Z","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.\r\nConsider the following simple example schema:\r\n\r\n```javascript\r\nvar FooSchema = new Schema({\r\n  bar: {\r\n    type: Schema.Types.ObjectId,\r\n    ref: 'Bar'\r\n  },\r\n  bars: [{\r\n    type: Schema.Types.ObjectId,\r\n    ref: 'Bar'\r\n  }]\r\n});\r\n```\r\n\r\nNow  suppose we get some Bar objects and we create a new Foo:\r\n\r\n```javascript\r\n//Assume these variables are Bar model instances with the following properties:\r\nvar bar1 = {id: 1, name: 'BarOne'};\r\nvar bar2 = {id: 2, name: 'BarTwo'};\r\nvar bar3 = {id: 3, name: 'BarThree'};\r\n\r\n//Prepare data for a new Foo instance:\r\nvar fooData = {\r\n  bar: bar1,\r\n  bars: [bar2, bar3]\r\n};\r\n\r\n//Create new Foo instance\r\nFoo.create(fooData).then(function(foo) {\r\n  //...\r\n});\r\n```\r\n\r\nAfter creation, the foo instance looks like this:\r\n\r\n```javascript\r\n{\r\n  bar: {\r\n    id: 1,\r\n    name: 'BarOne'\r\n  },\r\n  bars: [2, 3]\r\n}\r\n```\r\n\r\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.\r\n\r\nWhy the inconsistency?\r\n\r\nI would expect the foo instance to either un-populate all properties, or keep them all populated, if the data is available, e.g.:\r\n\r\n```javascript\r\n{\r\n  bar: 1,\r\n  bars: [2, 3]\r\n}\r\n```\r\n\r\nor\r\n\r\n```javascript\r\n{\r\n  bar: {\r\n    id: 1,\r\n    name: 'BarOne'\r\n  },\r\n  bars: [{\r\n    id: 2,\r\n    name: 'BarTwo'\r\n  }, {\r\n    id: 3,\r\n    name: 'BarThree'\r\n  }]\r\n}\r\n```\r\n\r\nAny way I can force this behaviour one way or another for both object refs and arrays of object refs?"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/157105327","html_url":"https://github.com/Automattic/mongoose/issues/3575#issuecomment-157105327","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3575","id":157105327,"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-11-16T17:19:43Z","updated_at":"2015-11-16T17:19:43Z","body":"Fix will be in 4.2.6 later today. The issue [is that it's tricky to support the general case of setting an array element to a populated value](https://github.com/Automattic/mongoose/issues/1530#issuecomment-59545311), because what happens with a partially-populated array? With this fix, it should work fine if you overwrite the entire array:\r\n\r\n```javascript\r\nvar barSchema = new mongoose.Schema({ name: String });\r\n\r\nvar Bar = mongoose.model('Bar', barSchema);\r\n\r\nvar fooSchema = new mongoose.Schema({\r\n  bar: {\r\n    type: Schema.Types.ObjectId,\r\n    ref: 'Bar'\r\n  },\r\n  bars: [{\r\n    type: Schema.Types.ObjectId,\r\n    ref: 'Bar'\r\n  }]\r\n});\r\n\r\nvar Foo = mongoose.model('Foo', fooSchema);\r\n\r\nBar.create([{ name: 'bar1' }, { name: 'bar2' }, { name: 'bar3' }], function(error, docs) {\r\n  assert.ifError(error);\r\n  assert.equal(docs.length, 3);\r\n  var foo = new Foo({ bar: docs[0], bars: [docs[1], docs[2]] });\r\n  console.log(foo);\r\n  process.exit(0);\r\n});\r\n```"}}