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.
$addToSethas the following syntax:
For more information on expressions, see Expressions.
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.
sales collection with the following documents:
Grouping the documents by the day and the year of the
date field, the following operation uses the
$addToSet accumulator to compute the list of unique items sold for each group:
The operation returns the following results: