发布于 2015-09-14 15:08:23 | 116 次阅读 | 评论: 0 | 来源: 网络整理
The $addToSet operator adds a value to an array only if the value is not in the array already. If the value is in the array, $addToSet returns without modifying the array. Consider the following example:
db.collection.update( { field: value }, { $addToSet: { field: value1 } } );
Here, $addToSet appends value1 to the array stored in field, only if value1 is not already a member of this array.
注解
$addToSet only ensures that there are no duplicate items in a set and makes no guarantees regarding the order of the elements in the set.