meteor/meteor — pull request #2671 by rzymek (2014-09-23 10:54:42+00:00)
Title: upsert $addToSet with $each field creation: $each inserted, not executed

### Issue ###
The `$each` modifier is inserted instead of being executed. Occurs when new field is created during upsert with `$addToSet`.

Executing an upsert:

{ $addToSet: { array: { $each: [] } } }
on an empty object (`{}`) yelds:

[{"_id":"wKGTDDytyxBnWA2sy","array":[{"$each":[]}]}]
instead of

[{"_id":"wKGTDDytyxBnWA2sy","array":[]}]

### Raw MongoDB ###
Executing on raw mongo:

db.meteor.addToSet.update({},{ $addToSet: { array: { $each: [] } } }, {upsert:true})
db.meteor.addToSet.find()

yelds, as expected:

[{"_id":"wKGTDDytyxBnWA2sy","array":[]}]

### Reproduction project ###
https://github.com/rzymek/meteor-issue-addToSet-each-upsert

Also this pull request includes test case, that fails without the fix.

### Implementation ###

Use the same `$each` handing code when creating a field as when updating one.