This version of the documentation is archived and no longer supported.



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 added to the set and does not affect existing duplicate elements. $addToSet does not guarantee a particular ordering of elements in the modified set.


The $each operator is available within the $addToSet, which allows you to add multiple values to the array if they do not exist in the field array in a single operation. For example:

db.collection.update( <query>,
                        $addToSet: { <field>: { $each: [ <value1>, <value2> ... ] } }

See also


←   $within $bit  →