How to Use $addToSet in Aggregation in MongoDB
Use
$addToSet in a MongoDB aggregation pipeline to add unique values to an array for each group. It ensures no duplicates are added when grouping documents with $group stage.Syntax
The $addToSet operator is used inside the $group stage of an aggregation pipeline. It adds unique values to an array for each group.
_id: The field or expression to group by.fieldName: { $addToSet: <expression> }: Adds unique values of the expression to an array.
json
{
$group: {
_id: <grouping_expression>,
uniqueValues: { $addToSet: <expression> }
}
}Example
This example groups documents by the "category" field and collects unique "item" names into an array called "uniqueItems".
mongodb
db.products.aggregate([
{
$group: {
_id: "$category",
uniqueItems: { $addToSet: "$item" }
}
}
])Output
[
{ "_id": "fruits", "uniqueItems": ["apple", "banana"] },
{ "_id": "vegetables", "uniqueItems": ["carrot", "lettuce"] }
]
Common Pitfalls
Common mistakes when using $addToSet include:
- Using
$addToSetoutside of a$groupstage, which will cause an error. - Expecting
$addToSetto preserve order; it does not guarantee order of elements. - Using
$pushwhen you want unique values;$pushadds duplicates.
mongodb
/* Wrong: Using $addToSet outside $group */ db.collection.aggregate([ { $addToSet: "$field" } ]) /* Right: Use inside $group */ db.collection.aggregate([ { $group: { _id: "$field", uniqueValues: { $addToSet: "$fieldToAdd" } } } ])
Quick Reference
| Operator | Purpose | Usage Context |
|---|---|---|
| $addToSet | Adds unique values to an array | Inside $group stage |
| $push | Adds values to an array (allows duplicates) | Inside $group stage |
| $group | Groups documents by specified key | Aggregation pipeline stage |
Key Takeaways
Use $addToSet inside $group to collect unique values into an array.
$addToSet prevents duplicate entries in the resulting array.
Do not use $addToSet outside of $group stage; it will cause errors.
$addToSet does not guarantee the order of elements in the array.
Use $push if you want to include duplicates instead of unique values.