- Reference >
- Operators >
- Aggregation Framework Operators >
- Group Aggregation Operators >
- $addToSet (aggregation)
$addToSet (aggregation)¶
On this page
-
$addToSet
¶ Returns an array of all the values found in the selected field among the documents in that group. Every unique value only appears once in the result set. There is no ordering guarantee for the output documents.
Example¶
In the mongo
shell, insert documents into a collection
named products
using the following operation:
Use the following db.collection.aggregate()
operation in the
mongo
shell with the $addToSet
operator:
This aggregation pipeline returns documents grouped on the value of
the type
field, with sets constructed from the unique values of
the price
and stocked
fields in the input documents. Consider
the following aggregation results: