{"action":"opened","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3315","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3315/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3315/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3315/events","html_url":"https://github.com/Automattic/mongoose/issues/3315","id":103817836,"number":3315,"title":"Strange behavior with Document.set(obj) and arrays (only sometimes)..","user":{"login":"zackurben","id":2689122,"avatar_url":"https://avatars.githubusercontent.com/u/2689122?v=3","gravatar_id":"","url":"https://api.github.com/users/zackurben","html_url":"https://github.com/zackurben","followers_url":"https://api.github.com/users/zackurben/followers","following_url":"https://api.github.com/users/zackurben/following{/other_user}","gists_url":"https://api.github.com/users/zackurben/gists{/gist_id}","starred_url":"https://api.github.com/users/zackurben/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zackurben/subscriptions","organizations_url":"https://api.github.com/users/zackurben/orgs","repos_url":"https://api.github.com/users/zackurben/repos","events_url":"https://api.github.com/users/zackurben/events{/privacy}","received_events_url":"https://api.github.com/users/zackurben/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-08-29T00:47:10Z","updated_at":"2015-08-29T00:47:10Z","closed_at":null,"body":"Hello, I have been experiencing some strange behavior with Document.set(<object>) and was wondering if I could get some feedback. I have searched for prior issues, and this seems like it could be possibly be related to: https://github.com/Automattic/mongoose/issues/1638\r\n\r\nThe test I have included does not show my exact use-case but this shows the exact behavior I am experiencing (I am having trouble isolating my exact issue).\r\n\r\nHere are my env versions:\r\n```\r\nmongodb: v3.0.3\r\nmongoose: v4.1.3\r\nnode: v0.12.6\r\n```\r\n\r\nThe following test has 3 variations (seen in test code):\r\n```javascript\r\ncase 1) var update = result; // works fine\r\ncase 2) var update = result.toObject(); // works fine\r\ncase 3) var update = _.clone(result._doc); // works fine\r\ncase 4) var update = result._doc; // broken, mangles permissions array\r\n```\r\n\r\nHere is my test:\r\n```javascript\r\n'use strict';\r\n\r\nvar mongoose = require('mongoose');\r\nvar _ = require('lodash');\r\nmongoose.connect('mongodb://localhost/testvalidation');\r\n\r\nvar echo = function(value) {\r\n  console.log('\\nPermissions:');\r\n  console.log(value);\r\n  return value;\r\n};\r\n\r\nvar PermissionSchema = {\r\n  _id: false,\r\n  type: {\r\n    type: String\r\n  },\r\n  roles: {\r\n    type: [mongoose.Schema.Types.ObjectId],\r\n    ref: 'role'\r\n  }\r\n};\r\nvar RoleSchema = new mongoose.Schema({ name: String });\r\nvar DocumentSchema = new mongoose.Schema({\r\n  permissions: {\r\n    type: [PermissionSchema],\r\n    set: echo\r\n  }\r\n});\r\n\r\nvar Role = mongoose.model('role', RoleSchema);\r\nvar Document = mongoose.model('document', DocumentSchema);\r\n\r\nvar r1 = new Role({ name: 'Admin' });\r\nvar r2 = new Role({ name: 'User' });\r\nvar d1 = new Document({\r\n  permissions: [\r\n    {type: 'create', roles: [r1._id, r2._id]},\r\n    {type: 'read', roles: [r1._id]}\r\n  ]\r\n});\r\n\r\nd1.save(function(err, result) {\r\n  if (err) throw err;\r\n  console.log();\r\n  console.log(result.permissions);\r\n\r\n  //var update = result; // works fine\r\n  //var update = result.toObject(); // works fine\r\n  //var update = _.clone(result._doc); // works fine\r\n  var update = result._doc; // broken, mangles permissions array\r\n  update.permissions = [\r\n    {type: 'update', roles: [r1._id]},\r\n    {type: 'create', roles: [r1._id]},\r\n    {type: 'read', roles: [r1._id]}\r\n  ];\r\n\r\n  result.set(update);\r\n  result.save(function(err, result) {\r\n    if (err) throw err;\r\n    console.log();\r\n    console.log(result.permissions);\r\n    mongoose.connection.close();\r\n  });\r\n});\r\n```\r\n\r\nOutput (for broken case):\r\n```\r\nPermissions:\r\n[ { type: 'create',\r\n    roles: [ 55e0fd04535ee2fd64ebdd9e, 55e0fd04535ee2fd64ebdd9f ] },\r\n  { type: 'read', roles: [ 55e0fd04535ee2fd64ebdd9e ] } ]\r\n\r\n[{ type: 'create',\r\n  roles: [ 55e0fd04535ee2fd64ebdd9e, 55e0fd04535ee2fd64ebdd9f ] }\r\n{ type: 'read', roles: [ 55e0fd04535ee2fd64ebdd9e ] }]\r\n\r\nPermissions:\r\n[ { type: 'update', roles: [ 55e0fd04535ee2fd64ebdd9e ] },\r\n  { type: 'create', roles: [ 55e0fd04535ee2fd64ebdd9e ] },\r\n  { type: 'read', roles: [ 55e0fd04535ee2fd64ebdd9e ] } ]\r\n\r\n[{ type: 'update', roles: [ 55e0fd04535ee2fd64ebdd9e ] }\r\n{ type: 'update', roles: [ 55e0fd04535ee2fd64ebdd9e ] }\r\n{ type: 'update', roles: [ 55e0fd04535ee2fd64ebdd9e ] }]\r\n```\r\n\r\nI believe this is unexpected output because the given key/value pairs are not fully mapped. The schema setter for the property is displaying the correct information, but only the first array element is actually being mapped (to every array element).\r\n\r\nThe difference between this test and my local issue is, the object I am using locally for `Document.set(<object>)` is not derived from the `Document._doc` object, however, I am getting the same results as seen above and I am trying to determine if my issue is related to this anomaly in any way.\r\n\r\nAny clues as to why 1/4 options fails, and why it occurs when they all display the same information during the setter function call?\r\n\r\nThanks in advance! "}}