{"action":"closed","issue":{"url":"https://api.github.com/repos/meteor/meteor/issues/4582","labels_url":"https://api.github.com/repos/meteor/meteor/issues/4582/labels{/name}","comments_url":"https://api.github.com/repos/meteor/meteor/issues/4582/comments","events_url":"https://api.github.com/repos/meteor/meteor/issues/4582/events","html_url":"https://github.com/meteor/meteor/issues/4582","id":89121563,"number":4582,"title":"mongo: upsert - order of $set and $setOnInsert in modifier matters","user":{"login":"mcbain","id":342739,"avatar_url":"https://avatars.githubusercontent.com/u/342739?v=3","gravatar_id":"","url":"https://api.github.com/users/mcbain","html_url":"https://github.com/mcbain","followers_url":"https://api.github.com/users/mcbain/followers","following_url":"https://api.github.com/users/mcbain/following{/other_user}","gists_url":"https://api.github.com/users/mcbain/gists{/gist_id}","starred_url":"https://api.github.com/users/mcbain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mcbain/subscriptions","organizations_url":"https://api.github.com/users/mcbain/orgs","repos_url":"https://api.github.com/users/mcbain/repos","events_url":"https://api.github.com/users/mcbain/events{/privacy}","received_events_url":"https://api.github.com/users/mcbain/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-17T20:44:10Z","updated_at":"2015-06-23T20:24:07Z","closed_at":"2015-06-23T20:24:07Z","body":"The following test does an upsert where the $set modifies a field on an object added by $setOnInsert. The $set overrides the value of `abc.hello` as expected. \r\n\r\n```javscript\r\nTinytest.add('upsert - order $setOnInsert before $set works', function (test) {\r\n  var collection = new Mongo.Collection(test.id);\r\n\r\n  collection.update(\r\n    {name: 'Meteor'},\r\n    {\r\n      $setOnInsert: {\r\n        abc: {\r\n          hello: 1\r\n        }\r\n      },\r\n      $set: {\r\n        'abc.hello': 33\r\n      }\r\n    },\r\n    {upsert: true}\r\n  );\r\n\r\n  var doc = collection.findOne();\r\n\r\n  test.equal(doc.abc.hello, 33);\r\n});\r\n```\r\n\r\nWhen changing the order of the two modifier fields the tests fails, because the value of `abc.hello` is still 1.  \r\n\r\n ```javscript\r\nTinytest.add('upsert - order $set before $setOnInsert works not', function (test) {\r\n  var collection = new Mongo.Collection(test.id);\r\n\r\n  collection.update(\r\n    {name: 'Meteor'},\r\n    {\r\n      $set: {\r\n        'abc.hello': 33\r\n      },\r\n      $setOnInsert: {\r\n        abc: {\r\n          hello: 1\r\n        }\r\n      }\r\n    },\r\n    {upsert: true}\r\n  );\r\n\r\n  var doc = collection.findOne();\r\n\r\n  test.equal(doc.abc.hello, 33);\r\n});\r\n```"}}