0
0
MongodbHow-ToBeginner · 3 min read

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 $addToSet outside of a $group stage, which will cause an error.
  • Expecting $addToSet to preserve order; it does not guarantee order of elements.
  • Using $push when you want unique values; $push adds 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

OperatorPurposeUsage Context
$addToSetAdds unique values to an arrayInside $group stage
$pushAdds values to an array (allows duplicates)Inside $group stage
$groupGroups documents by specified keyAggregation 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.