Skip to content

Aggregation Pipeline

Aryeh Citron edited this page Apr 26, 2026 · 1 revision

Aggregation Pipeline

Stages

The following 34 aggregation pipeline stages are supported:

Stage Description
$match Filter documents
$project Reshape documents (include, exclude, compute fields)
$addFields / $set Add or overwrite fields
$unset Remove fields
$group Group by key with accumulators
$sort Sort documents
$limit Limit number of documents
$skip Skip documents
$unwind Deconstruct array field
$lookup Left outer join to another collection
$replaceRoot / $replaceWith Replace document with a sub-document
$count Count documents
$sortByCount Group, count, and sort
$sample Random sample
$facet Multiple sub-pipelines in parallel
$bucket Categorize into buckets
$bucketAuto Auto-bucket by count
$unionWith Union with another collection
$graphLookup Recursive graph lookup
$redact Restrict document content
$merge Write results to a collection
$out Write results to a collection (replace)
$setWindowFields Window functions
$densify Fill gaps in sequence
$fill Fill missing values
$documents Provide literal documents
$collStats Collection statistics
$indexStats Index statistics
$geoNear Geospatial near query
$search Atlas text search (basic substring support)
$searchMeta Atlas search metadata (stub)
$vectorSearch Vector similarity search (brute-force)

Group Accumulators

Accumulator Description
$sum Sum of values
$avg Average of values
$min Minimum value
$max Maximum value
$first First value in group
$last Last value in group
$push Array of all values
$addToSet Array of unique values
$count Count of documents
$mergeObjects Merge documents
$stdDevPop Population standard deviation
$stdDevSamp Sample standard deviation
$top / $bottom Top/bottom by sort
$topN / $bottomN Top/bottom N by sort
$firstN / $lastN First/last N values
$maxN / $minN Max/min N values
$accumulator Custom JavaScript accumulator (requires JsTriggers)

Window Functions

Available in $setWindowFields:

$sum, $avg, $min, $max, $count, $push, $addToSet, $first, $last, $stdDevPop, $stdDevSamp, $rank, $denseRank, $documentNumber, $shift, $expMovingAvg, $derivative, $integral, $covariancePop, $covarianceSamp, $linearFill, $locf

Expression Operators

Arithmetic (16)

$add, $subtract, $multiply, $divide, $mod, $abs, $ceil, $floor, $round, $trunc, $pow, $sqrt, $log, $log10, $ln, $exp

Trigonometric (9)

$sin, $cos, $tan, $asin, $acos, $atan, $atan2, $degreesToRadians, $radiansToDegrees

String (20)

$concat, $toLower, $toUpper, $trim, $ltrim, $rtrim, $substr, $substrBytes, $substrCP, $strLenBytes, $strLenCP, $indexOfBytes, $indexOfCP, $split, $strcasecmp, $replaceOne, $replaceAll, $regexMatch, $regexFind, $regexFindAll

Comparison (7)

$cmp, $eq, $ne, $gt, $gte, $lt, $lte

Conditional (3)

$cond, $ifNull, $switch

Logical (3)

$and, $or, $not

Array (17)

$arrayElemAt, $size, $isArray, $concatArrays, $in, $filter, $map, $reduce, $reverseArray, $range, $slice, $first, $last, $sortArray, $objectToArray, $arrayToObject, $zip

Type & Conversion (11)

$type, $convert, $toBool, $toInt, $toLong, $toDouble, $toDecimal, $toString, $toDate, $toObjectId, $isNumber

Date (14)

$year, $month, $dayOfMonth, $hour, $minute, $second, $millisecond, $dayOfWeek, $dayOfYear, $dateToString, $dateFromString, $dateAdd, $dateSubtract, $dateDiff

Object (3)

$mergeObjects, $getField, $setField

Special (8)

$literal, $let, $binarySize, $bsonSize, $rand, $sampleRate, $toHashedIndexKey, $function (requires JsTriggers)

Timestamp (2)

$tsIncrement, $tsSecond

See Also

Clone this wiki locally