$addToSet
The $addToSet aggregation operator returns an array of unique values from a specified expression for each group. It is used within the $group stage to accumulate distinct values, automatically eliminating duplicates.
Parameters
-
expression: The expression to evaluate for each document in the group.
Example (MongoDB Shell)
The following example demonstrates using the $addToSet operator to collect unique cities where orders were placed for each customer.
Create sample documents
db.orders.insertMany([ { _id: 1, customer: "Alice", city: "Seattle", amount: 100 }, { _id: 2, customer: "Alice", city: "Portland", amount: 150 }, { _id: 3, customer: "Bob", city: "Seattle", amount: 200 }, { _id: 4, customer: "Alice", city: "Seattle", amount: 75 }, { _id: 5, customer: "Bob", city: "Boston", amount: 300 } ]);
Query example
db.orders.aggregate([ { $group: { _id: "$customer", cities: { $addToSet: "$city" } } } ]);
Output
[
{ _id: 'Bob', cities: [ 'Seattle', 'Boston' ] },
{ _id: 'Alice', cities: [ 'Seattle', 'Portland' ] }
]
Code examples
To view a code example for using the $addToSet aggregation operator, choose the tab for the language that you want to use: