Aggregation Pipeline Operators

Note

For details on specific operator, including syntax and examples,click on the specific operator to go to its reference page.

Expression Operators

These expression operators are available to construct expressions for use in the aggregation pipelinestages.

Operator expressions are similar to functions that take arguments.In general, these expressions take an array of argumentsand have the following form:

  1. { <operator>: [ <argument1>, <argument2> ... ] }

If operator accepts a single argument, you can omit the outer arraydesignating the argument list:

  1. { <operator>: <argument> }

To avoid parsing ambiguity if the argument is a literal array, you mustwrap the literal array in a $literal expression or keepthe outer array that designates the argument list.

Arithmetic Expression Operators

Arithmetic expressions perform mathematic operations on numbers. Somearithmetic expressions can also support date arithmetic.

NameDescription
$absReturns the absolute value of a number.
$addAdds numbers to return the sum, or adds numbers and a date toreturn a new date. If adding numbers and a date, treats thenumbers as milliseconds. Accepts any number of argumentexpressions, but at most, one expression can resolve to adate.
$ceilReturns the smallest integer greater than or equal to the specified number.
$divideReturns the result of dividing the first number by thesecond. Accepts two argument expressions.
$expRaises e to the specified exponent.
$floorReturns the largest integer less than or equal to thespecified number.
$lnCalculates the natural log of a number.
$logCalculates the log of a number in the specified base.
$log10Calculates the log base 10 of a number.
$modReturns the remainder of the first number divided by thesecond. Accepts two argument expressions.
$multiplyMultiplies numbers to return the product. Accepts any numberof argument expressions.
$powRaises a number to the specified exponent.
$roundRounds a number to to a whole integer or to a specifieddecimal place.
$sqrtCalculates the square root.
$subtractReturns the result of subtracting the second value from thefirst. If the two values are numbers, return the difference.If the two values are dates, return the difference inmilliseconds. If the two values are a date and a number inmilliseconds, return the resulting date. Accepts two argumentexpressions. If the two values are a date and a number,specify the date argument first as it is not meaningful tosubtract a date from a number.
$truncTruncates a number to a whole integer or to a specifieddecimal place.

Array Expression Operators

$arrayElemAtReturns the element at the specified array index.
$arrayToObjectConverts an array of key value pairs to a document.
$concatArraysConcatenates arrays to return the concatenated array.
$filterSelects a subset of the array to return an array with onlythe elements that match the filter condition.
$inReturns a boolean indicating whether a specified value is inan array.
$indexOfArraySearches an array for an occurence of a specified value andreturns the array index of the first occurence. If thesubstring is not found, returns -1.
$isArrayDetermines if the operand is an array. Returns a boolean.
$mapApplies a subexpression to each element of an array andreturns the array of resulting values in order. Accepts namedparameters.
$objectToArrayConverts a document to an array of documents representingkey-value pairs.
$rangeOutputs an array containing a sequence of integers accordingto user-defined inputs.
$reduceApplies an expression to each element in an array andcombines them into a single value.
$reverseArrayReturns an array with the elements in reverse order.
$sizeReturns the number of elements in the array. Accepts a singleexpression as argument.
$sliceReturns a subset of an array.
$zipMerge two arrays together.

Boolean Expression Operators

Boolean expressions evaluate their argument expressions as booleans andreturn a boolean as the result.

In addition to the false boolean value, Boolean expression evaluatesas false the following: null, 0, and undefinedvalues. The Boolean expression evaluates all other values as true,including non-zero numeric values and arrays.

NameDescription
$andReturns true only when all its expressions evaluate totrue. Accepts any number of argument expressions.
$notReturns the boolean value that is the opposite of itsargument expression. Accepts a single argument expression.
$orReturns true when any of its expressions evaluates totrue. Accepts any number of argument expressions.

Comparison Expression Operators

Comparison expressions return a boolean except for $cmpwhich returns a number.

The comparison expressions take two argument expressions and compareboth value and type, using the specified BSON comparison order for values of different types.

$cmpReturns 0 if the two values are equivalent, 1 if thefirst value is greater than the second, and -1 if thefirst value is less than the second.
$eqReturns true if the values are equivalent.
$gtReturns true if the first value is greater than thesecond.
$gteReturns true if the first value is greater than or equalto the second.
$ltReturns true if the first value is less than the second.
$lteReturns true if the first value is less than or equal tothe second.
$neReturns true if the values are not equivalent.

Conditional Expression Operators

NameDescription
$condA ternary operator that evaluates one expression, anddepending on the result, returns the value of one of theother two expressions. Accepts either three expressions in anordered list or three named parameters.
$ifNullReturns either the non-null result of the first expression orthe result of the second expression if the first expressionresults in a null result. Null result encompasses instancesof undefined values or missing fields. Accepts twoexpressions as arguments. The result of the second expressioncan be null.
$switchEvaluates a series of case expressions. When it finds anexpression which evaluates to true, $switch executesa specified expression and breaks out of the control flow.

Date Expression Operators

The following operators returns date objects or components of a dateobject:

NameDescription
$dateFromPartsConstructs a BSON Date object given the date’s constituentparts.
$dateFromStringConverts a date/time string to a date object.
$dateToPartsReturns a document containing the constituent parts of a date.
$dateToStringReturns the date as a formatted string.
$dayOfMonthReturns the day of the month for a date as a number between 1and 31.
$dayOfWeekReturns the day of the week for a date as a number between 1(Sunday) and 7 (Saturday).
$dayOfYearReturns the day of the year for a date as a number between 1and 366 (leap year).
$hourReturns the hour for a date as a number between 0 and 23.
$isoDayOfWeekReturns the weekday number in ISO 8601 format, ranging from1 (for Monday) to 7 (for Sunday).
$isoWeekReturns the week number in ISO 8601 format, ranging from1 to 53. Week numbers start at 1 with the week(Monday through Sunday) that contains the year’s firstThursday.
$isoWeekYearReturns the year number in ISO 8601 format. The year startswith the Monday of week 1 (ISO 8601) and ends with the Sundayof the last week (ISO 8601).
$millisecondReturns the milliseconds of a date as a number between 0 and999.
$minuteReturns the minute for a date as a number between 0 and 59.
$monthReturns the month for a date as a number between 1 (January)and 12 (December).
$secondReturns the seconds for a date as a number between 0 and 60(leap seconds).
$toDateConverts value to a Date.New in version 4.0.
$weekReturns the week number for a date as a number between 0 (thepartial week that precedes the first Sunday of the year) and53 (leap year).
$yearReturns the year for a date as a number (e.g. 2014).

The following arithmetic operators can take date operands:

NameDescription
$addAdds numbers and a date to return a new date. If addingnumbers and a date, treats the numbers as milliseconds.Accepts any number of argument expressions, but at most, oneexpression can resolve to a date.
$subtractReturns the result of subtracting the second value from thefirst. If the two values are dates, return the difference inmilliseconds. If the two values are a date and a number inmilliseconds, return the resulting date. Accepts two argumentexpressions. If the two values are a date and a number,specify the date argument first as it is not meaningful tosubtract a date from a number.

Literal Expression Operator

NameDescription
$literalReturn a value without parsing. Use for values that theaggregation pipeline may interpret as an expression. Forexample, use a $literal expression to a stringthat starts with a $ to avoid parsing as a field path.

Object Expression Operators

NameDescription
$mergeObjectsCombines multiple documents into a single document.New in version 3.6.
$objectToArrayConverts a document to an array of documents representingkey-value pairs.New in version 3.6.

Set Expression Operators

Set expressions performs set operation on arrays, treating arrays assets. Set expressions ignores the duplicate entries in each input arrayand the order of the elements.

If the set operation returns a set, the operation filters outduplicates in the result to output an array that contains only uniqueentries. The order of the elements in the output array is unspecified.

If a set contains a nested array element, the set expression does not descendinto the nested array but evaluates the array at top-level.

NameDescription
$allElementsTrueReturns true if no element of a set evaluates tofalse, otherwise, returns false. Accepts a singleargument expression.
$anyElementTrueReturns true if any elements of a set evaluate totrue; otherwise, returns false. Accepts a singleargument expression.
$setDifferenceReturns a set with elements that appear in the first setbut not in the second set; i.e. performs a relativecomplement)of the second set relative to the first. Accepts exactlytwo argument expressions.
$setEqualsReturns true if the input sets have the same distinctelements. Accepts two or more argument expressions.
$setIntersectionReturns a set with elements that appear in all of theinput sets. Accepts any number of argument expressions.
$setIsSubsetReturns true if all elements of the first set appearin the second set, including when the first set equals thesecond set; i.e. not a strict subset. Accepts exactlytwo argument expressions.
$setUnionReturns a set with elements that appear in any of theinput sets.

String Expression Operators

String expressions, with the exception of$concat, only have a well-defined behavior for strings of ASCII characters.

$concat behavior is well-defined regardless ofthe characters used.

NameDescription
$concatConcatenates any number of strings.
$dateFromStringConverts a date/time string to a date object.
$dateToStringReturns the date as a formatted string.
$indexOfBytesSearches a string for an occurence of a substring and returnsthe UTF-8 byte index of the first occurence. If the substringis not found, returns -1.
$indexOfCPSearches a string for an occurence of a substring and returnsthe UTF-8 code point index of the first occurence. If thesubstring is not found, returns -1
$ltrimRemoves whitespace or the specified characters from thebeginning of a string.New in version 4.0.
$regexFindApplies a regular expression (regex) to a string and returnsinformation on the first matched substring.New in version 4.2.
$regexFindAllApplies a regular expression (regex) to a string and returnsinformation on the all matched substrings.New in version 4.2.
$regexMatchApplies a regular expression (regex) to a string and returnsa boolean that indicates if a match is found or not.New in version 4.2.
$rtrimRemoves whitespace or the specified characters from theend of a string.New in version 4.0.
$splitSplits a string into substrings based on a delimiter. Returnsan array of substrings. If the delimiter is not found withinthe string, returns an array containing the original string.
$strLenBytesReturns the number of UTF-8 encoded bytes in a string.
$strLenCPReturns the number of UTF-8 code points in a string.
$strcasecmpPerforms case-insensitive string comparison and returns:0 if two strings are equivalent, 1 if the firststring is greater than the second, and -1 if the firststring is less than the second.
$substrDeprecated. Use $substrBytes or$substrCP.
$substrBytesReturns the substring of a string. Starts with the characterat the specified UTF-8 byte index (zero-based) in the stringand continues for the specified number of bytes.
$substrCPReturns the substring of a string. Starts with the characterat the specified UTF-8 code point (CP) index(zero-based) in the string and continues for the number ofcode points specified.
$toLowerConverts a string to lowercase. Accepts a single argumentexpression.
$toStringConverts value to a string.New in version 4.0.
$trimRemoves whitespace or the specified characters from thebeginning and end of a string.New in version 4.0.
$toUpperConverts a string to uppercase. Accepts a single argumentexpression.

Text Expression Operator

NameDescription
$metaAccess text search metadata.

Trigonometry Expression Operators

Trigonometry expressions perform trigonometric operations on numbers.Values that represent angles are always input or output in radians. Use$degreesToRadians and $radiansToDegrees toconvert between degree and radian measurements.

NameDescription
$sinReturns the sine of a value that is measured in radians.
$cosReturns the cosine of a value that is measured in radians.
$tanReturns the tangent of a value that is measured in radians.
$asinReturns the inverse sin (arc sine) of a value in radians.
$acosReturns the inverse cosine (arc cosine) of a value in radians.
$atanReturns the inverse tangent (arc tangent) of a value inradians.
$atan2Returns the inverse tangent (arc tangent) of y / x inradians, where y and x are the first and secondvalues passed to the expression respectively.
$asinhReturns the inverse hyperbolic sine (hyperbolic arc sine) of avalue in radians.
$acoshReturns the inverse hyperbolic cosine (hyperbolic arc cosine)of a value in radians.
$atanhReturns the inverse hyperbolic tangent (hyperbolic arctangent) of a value in radians.
$degreesToRadiansConverts a value from degrees to radians.
$radiansToDegreesConverts a value from radians to degrees.

Type Expression Operators

NameDescription
$convertConverts a value to a specified type.New in version 4.0.
$toBoolConverts value to a boolean.New in version 4.0.
$toDateConverts value to a Date.New in version 4.0.
$toDecimalConverts value to a Decimal128.New in version 4.0.
$toDoubleConverts value to a double.New in version 4.0.
$toIntConverts value to an integer.New in version 4.0.
$toLongConverts value to a long.New in version 4.0.
$toObjectIdConverts value to an ObjectId.New in version 4.0.
$toStringConverts value to a string.New in version 4.0.
$typeReturn the BSON data type of the field.

Accumulators ($group)

Available for use in the $group stage, accumulators areoperators that maintain their state (e.g. totals, maximums, minimums,and related data) as documents progress through the pipeline.

When used as accumulators in the $group stage, theseoperators take as input a single expression, evaluating the expressiononce for each input document, and maintain their stage for the group ofdocuments that share the same group key.

NameDescription
$addToSetReturns an array of unique expression values for eachgroup. Order of the array elements is undefined.
$avgReturns an average of numerical values. Ignores non-numeric values.
$firstReturns a value from the first document for each group. Orderis only defined if the documents are in a defined order.
$lastReturns a value from the last document for each group. Orderis only defined if the documents are in a defined order.
$maxReturns the highest expression value for each group.
$mergeObjectsReturns a document created by combining the input documentsfor each group.
$minReturns the lowest expression value for each group.
$pushReturns an array of expression values for each group.
$stdDevPopReturns the population standard deviation of the input values.
$stdDevSampReturns the sample standard deviation of the input values.
$sumReturns a sum of numerical values. Ignores non-numeric values.

Accumulators (in Other Stages)

Some operators that are available as accumulators for the$group stage are also available for use in other stages butnot as accumulators. When used in these other stages, these operatorsdo not maintain their state and can take as input either a singleargument or multiple arguments. For details, refer to the specificoperator page.

Changed in version 3.2.

The following accumulator operators are also available in the$project, $addFields, and $set stages.

NameDescription
$avgReturns an average of the specified expression or list ofexpressions for each document. Ignores non-numeric values.
$maxReturns the maximum of the specified expression or list ofexpressions for each document
$minReturns the minimum of the specified expression or list ofexpressions for each document
$stdDevPopReturns the population standard deviation of the input values.
$stdDevSampReturns the sample standard deviation of the input values.
$sumReturns a sum of numerical values. Ignores non-numeric values.

Variable Expression Operators

NameDescription
$letDefines variables for use within the scope of a subexpressionand returns the result of the subexpression. Accepts namedparameters.Accepts any number of argument expressions.

Alphabetical Listing of Expression Operators

NameDescription
$absReturns the absolute value of a number.
$acosReturns the inverse cosine (arc cosine) of a value in radians.
$acoshReturns the inverse hyperbolic cosine (hyperbolic arc cosine) of a value in radians.
$addAdds numbers to return the sum, or adds numbers and a date to returna new date. If adding numbers and a date, treats the numbers asmilliseconds. Accepts any number of argument expressions, but atmost, one expression can resolve to a date.
$addToSetReturns an array of unique expression values for each group. Order of thearray elements is undefined.Available in $group stage only.
$allElementsTrueReturns true if no element of a set evaluates to false,otherwise, returns false. Accepts a single argument expression.
$andReturns true only when all its expressions evaluate totrue. Accepts any number of argument expressions.
$anyElementTrueReturns true if any elements of a set evaluate to true;otherwise, returns false. Accepts a single argument expression.
$arrayElemAtReturns the element at the specified array index.
$arrayToObjectConverts an array of key value pairs to a document.
$asinReturns the inverse sine (arc sine) of a value in radians.
$asinhReturns the inverse hyperbolic sin (hyperbolic arc sine) of a value in radians.
$atanReturns the inverse tangent (arc tangent) of a value in radians.
$atan2Returns the inverse tangent (arc tangent) of y / x in radians,where y and x are the first and second values passed to theexpression respectively.
$atanhReturns the inverse hyperbolic tangent (hyperbolic arc tangent) of a value in radians.
$avgReturns an average of numerical values. Ignores non-numeric values.Changed in version 3.2: Available in both $group and $projectstages.
$ceilReturns the smallest integer greater than or equal to the specified number.
$cmpReturns: 0 if the two values areequivalent, 1 if the first value is greater than thesecond, and -1 if the first value is less than thesecond.
$concatConcatenates any number of strings.
$concatArraysConcatenates arrays to return the concatenated array.
$condA ternary operator that evaluates one expression, and depending onthe result, returns the value of one of the other two expressions.Accepts either three expressions in an ordered list or three namedparameters.
$convertConverts a value to a specified type.
$cosReturns the cosine of a value that is measured in radians.
$dateFromPartsConstructs a BSON Date object given the date’s constituentparts.
$dateToPartsReturns a document containing the constituent parts of a date.
$dateFromStringReturns a date/time as a date object.
$dateToStringReturns the date as a formatted string.
$dayOfMonthReturns the day of the month for a date as a number between 1 and 31.
$dayOfWeekReturns the day of the week for a date as a number between 1 (Sunday)and 7 (Saturday).
$dayOfYearReturns the day of the year for a date as a number between 1 and 366(leap year).
$degreesToRadiansConverts a value from degrees to radians.
$divideReturns the result of dividing the first number by the second. Acceptstwo argument expressions.
$eqReturns true if the values areequivalent.
$expRaises e to the specified exponent.
$filterSelects a subset of the array to return an array with only the elementsthat match the filter condition.
$firstReturns a value from the first document for each group. Order isonly defined if the documents are in a defined order.Available in $group stage only.
$floorReturns the largest integer less than or equal to the specified number.
$gtReturns true if the first value isgreater than the second.
$gteReturns true if the first value isgreater than or equal to the second.
$hourReturns the hour for a date as a number between 0 and 23.
$ifNullReturns either the non-null result of the first expression or theresult of the second expression if the first expression results in anull result. Null result encompasses instances of undefined values ormissing fields. Accepts two expressions as arguments. The result ofthe second expression can be null.
$inReturns a boolean indicating whether a specified value is in an array.
$indexOfArraySearches an array for an occurence of a specified value and returnsthe array index of the first occurence. If the substring is not found,returns -1.
$indexOfBytesSearches a string for an occurence of a substring and returns theUTF-8 byte index of the first occurence. If the substring is notfound, returns -1.
$indexOfCPSearches a string for an occurence of a substring and returns theUTF-8 code point index of the first occurence. If thesubstring is not found, returns -1.
$isArrayDetermines if the operand is an array. Returns a boolean.
$isoDayOfWeekReturns the weekday number in ISO 8601 format, ranging from1 (for Monday) to 7 (for Sunday).
$isoWeekReturns the week number in ISO 8601 format, rangingfrom 1 to 53. Weeknumbers start at 1 with the week (Monday through Sunday)that contains the year’s first Thursday.
$isoWeekYearReturns the year number in ISO 8601 format. The year startswith the Monday of week 1 (ISO 8601) and ends with the Sunday of thelast week (ISO 8601).
$lastReturns a value from the last document for each group. Order isonly defined if the documents are in a defined order.Available in $group stage only.
$letDefines variables for use within the scope of a subexpression andreturns the result of the subexpression. Accepts named parameters.Accepts any number of argument expressions.
$literalReturn a value without parsing. Use for values that the aggregationpipeline may interpret as an expression. For example, use a$literal expression to a string that starts with a$ to avoid parsing as a field path.
$lnCalculates the natural log of a number.
$logCalculates the log of a number in the specified base.
$log10Calculates the log base 10 of a number.
$ltReturns true if the first value is lessthan the second.
$lteReturns true if the first value is lessthan or equal to the second.
$ltrimRemoves whitespace or the specified characters from the beginning of a string.
$mapApplies a subexpression to each element of an array and returns thearray of resulting values in order. Accepts named parameters.
$maxReturns the highest expression value for each group.Changed in version 3.2: Available in both $group and $projectstages.
$mergeObjectsCombines multiple documents into a single document.
$metaAccess text search metadata.
$minReturns the lowest expression value for each group.Changed in version 3.2: Available in both $group and $projectstages.
$millisecondReturns the milliseconds of a date as a number between 0and 999.
$minuteReturns the minute for a date as a number between 0 and 59.
$modReturns the remainder of the first number divided by the second.Accepts two argument expressions.
$monthReturns the month for a date as a number between 1(January) and 12 (December).
$multiplyMultiplies numbers to return the product. Accepts any number ofargument expressions.
$neReturns true if the values are not_equivalent.
$notReturns the boolean value that is the opposite of its argumentexpression. Accepts a single argument expression.
$objectToArrayConverts a document to an array of documents representing key-value pairs.
$orReturns true when _any of its expressions evaluates to true.Accepts any number of argument expressions.
$powRaises a number to the specified exponent.
$pushReturns an array of expression values for each group.Available in $group stage only.
$radiansToDegreesConverts a value from radians to degrees.
$rangeOutputs an array containing a sequence of integers according touser-defined inputs.
$reduceApplies an expression to each element in an array and combines theminto a single value.
$regexFindApplies a regular expression (regex) to a string and returnsinformation on the first matched substring.
$regexFindAllApplies a regular expression (regex) to a string and returnsinformation on the all matched substrings.
$regexMatchApplies a regular expression (regex) to a string and returnsa boolean that indicates if a match is found or not.
$reverseArrayReturns an array with the elements in reverse order.
$roundRounds a number to a whole integer or to a specified decimal place.
$rtrimRemoves whitespace or the specified characters from the end of a string.
$secondReturns the seconds for a date as a number between 0 and 60(leap seconds).
$setDifferenceReturns a set with elements that appear in the first set but not inthe second set; i.e. performs arelative complement)of the second set relative to the first. Accepts exactly twoargument expressions.
$setEqualsReturns true if the input sets have the same distinct elements.Accepts two or more argument expressions.
$setIntersectionReturns a set with elements that appear in all of the input sets.Accepts any number of argument expressions.
$setIsSubsetReturns true if all elements of the first set appear in thesecond set, including when the first set equals the second set; i.e.not a strict subset.Accepts exactly two argument expressions.
$setUnionReturns a set with elements that appear in any of the input sets.
$sizeReturns the number of elements in the array. Accepts a singleexpression as argument.
$sinReturns the sine of a value that is measured in radians.
$sliceReturns a subset of an array.
$splitSplits a string into substrings based on a delimiter. Returns anarray of substrings. If the delimiter is not found within the string,returns an array containing the original string.
$sqrtCalculates the square root.
$stdDevPopReturns the population standard deviation of the input values.Changed in version 3.2: Available in both $group and $projectstages.
$stdDevSampReturns the sample standard deviation of the input values.Changed in version 3.2: Available in both $group and $projectstages.
$strcasecmpPerforms case-insensitive string comparison and returns: 0 if twostrings are equivalent, 1 if the first string is greater than thesecond, and -1 if the first string is less than the second.
$strLenBytesReturns the number of UTF-8 encoded bytes in a string.
$strLenCPReturns the number of UTF-8 code points in a string.
$substrDeprecated. Use $substrBytes or $substrCP.
$substrBytesReturns the substring of a string. Starts with thecharacter at the specified UTF-8 byte index (zero-based) in the stringand continues for the specified number of bytes.
$substrCPReturns the substring of a string. Starts with thecharacter at the specified UTF-8 code point (CP) index (zero-based)in the string and continues for the number of code points specified.
$subtractReturns the result of subtracting the second value from the first. Ifthe two values are numbers, return the difference. If the two valuesare dates, return the difference in milliseconds. If the two valuesare a date and a number in milliseconds, return the resulting date.Accepts two argument expressions. If the two values are a date and anumber, specify the date argument first as it is not meaningful tosubtract a date from a number.
$sumReturns a sum of numerical values. Ignores non-numeric values.Changed in version 3.2: Available in both $group and $projectstages.
$switchEvaluates a series of case expressions. When it finds an expressionwhich evaluates to true, $switch executes a specifiedexpression and breaks out of the control flow.
$tanReturns the tangent of a value that is measured in radians.
$toBoolConverts value to a boolean.
$toDateConverts value to a Date.
$toDecimalConverts value to a Decimal128.
$toDoubleConverts value to a double.
$toIntConverts value to an integer.
$toLongConverts value to a long.
$toObjectIdConverts value to an ObjectId.
$toStringConverts value to a string.
$toLowerConverts a string to lowercase. Accepts a single argument expression.
$toUpperConverts a string to uppercase. Accepts a single argument expression.
$trimRemoves whitespace or the specified characters from the beginningand end of a string.
$truncTruncates a number to a whole integer or to a specified decimal place.
$typeReturn the BSON data type of the field.
$weekReturns the week number for a date as a number between 0 (the partialweek that precedes the first Sunday of the year) and 53 (leap year).
$yearReturns the year for a date as a number (e.g. 2014).
$zipMerge two arrays together.

For the pipeline stages, seeAggregation Pipeline Stages.