- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $addToSet (aggregation)
$addToSet (aggregation)¶
On this page
Definition¶
-
$addToSet
¶ Returns an array of all unique values that results from applying an expression to each document in a group of documents that share the same group by key. Order of the elements in the output array is unspecified.
$addToSet
is only available in the$group
stage.$addToSet
has the following syntax:For more information on expressions, see Expressions.
Behavior¶
If the value of the expression is an array, $addToSet
appends
the whole array as a single element.
If the value of the expression is a document, MongoDB determines that the document is a duplicate if another document in the array matches the to-be-added document exactly; i.e. the existing document has the exact same fields and values in the exact same order.