Problem Statement
What are accumulator operators in MongoDB aggregation and when would you use $push vs $addToSet?
Explanation
Accumulator operators are used within the dollar group stage to compute values across grouped documents. Common accumulators include dollar sum for totals, dollar avg for averages, dollar min and dollar max for extremes, dollar first and dollar last for boundary values, dollar push and dollar addToSet for arrays.
The dollar push accumulator creates an array containing all values from the grouped documents, including duplicates. Use dollar push when you need to collect all values, even if some are repeated. For example, collecting all order dates for a customer where some dates might repeat.
The dollar addToSet accumulator creates an array of unique values, automatically removing duplicates. Use dollar addToSet when you only want distinct values. For example, collecting all unique product categories a customer has purchased from.
The key difference is that dollar push includes duplicates while dollar addToSet ensures uniqueness. Both operators are useful for creating arrays during grouping, but choose based on whether you need to preserve or eliminate duplicate values in your analysis.
Code Solution
SolutionRead Only
// $push - includes duplicates
db.orders.aggregate([
{
$group: {
_id: "$customerId",
allDates: { $push: "$orderDate" }
}
}
])
// Result: allDates: ["2024-01-01", "2024-01-01", "2024-02-01"]
// $addToSet - unique values only
db.orders.aggregate([
{
$group: {
_id: "$customerId",
categories: { $addToSet: "$category" }
}
}
])
// Result: categories: ["Electronics", "Books"]