Can I $addToSet and upsert an array? Can I $addToSet and upsert an array? mongoose mongoose

Can I $addToSet and upsert an array?


This Behavior is now supported by Mongodb 3.2

From MongoDB documentation:If the field is absent in the document to update, $addToSet creates the array field with the specified value as its element.

https://docs.mongodb.org/manual/reference/operator/update/addToSet/


You either have to initialize your account document with an empty accounts array on first creation or use $push if your first $addToSet fails. $push creates a new field if the field is absent, which $addToSet does not.