Aggregation Operators in MongoDB

From the MongoDB cheat sheet ยท Aggregation Pipeline ยท verified Jul 2026

Aggregation Operators

Operators for calculations and transformations

javascript
// Group accumulator operators
db.sales.aggregate([
  { $group: {
    _id: "$category",
    total: { $sum: "$amount" },
    average: { $avg: "$amount" },
    min: { $min: "$amount" },
    max: { $max: "$amount" },
    count: { $sum: 1 },
    items: { $push: "$item" },
    uniqueItems: { $addToSet: "$item" }
  }}
])

// String operators
{ $project: {
  upper: { $toUpper: "$name" },
  lower: { $toLower: "$name" },
  substring: { $substr: ["$name", 0, 3] },
  concat: { $concat: ["$first", " ", "$last"] }
}}
๐Ÿ’ก $sum: 1 counts documents
๐Ÿ“Œ Use $$ for variables in expressions
โšก $cond for if-then-else logic
๐ŸŸข Essential for data analysis
๐Ÿ”— Related: $map, $reduce for array processing
aggregationoperators

More MongoDB tasks

Back to the full MongoDB cheat sheet