Aggregation Pipeline Operators聚合管道运算符
For details on a specific operator, including syntax and examples, click on the link to the operator's reference page.有关特定运算符的详细信息,包括语法和示例,请单击该运算符参考页的链接。
Expression Operators表达式运算符
These expression operators are available to construct expressions for use in the aggregation pipeline stages.这些表达式运算符可用于构造用于聚合管道阶段的表达式。
Operator expressions are similar to functions that take arguments. In general, these expressions take an array of arguments and have the following form:运算符表达式类似于接受参数的函数。通常,这些表达式采用参数数组,并具有以下形式:
{ <operator>: [ <argument1>, <argument2> ... ] }
If operator accepts a single argument, you can omit the outer array designating the argument list:如果运算符接受单个参数,则可以省略指定参数列表的外部数组:
{ <operator>: <argument> }
To avoid parsing ambiguity if the argument is a literal array, you must wrap the literal array in a 如果参数是文字数组,为了避免解析歧义,必须将文字数组包装在$literal
expression or keep the outer array that designates the argument list.$literal
表达式中,或者保留指定参数列表的外部数组。
Arithmetic Expression Operators算术表达式运算符
Arithmetic expressions perform mathematic operations on numbers. 算术表达式对数字进行数学运算。Some arithmetic expressions can also support date arithmetic.一些算术表达式也可以支持日期算术。
$abs | |
$add | |
$ceil | |
$divide | |
$exp | |
$floor | |
$ln | |
$log | |
$log10 | |
$mod | |
$multiply | |
$pow | |
$round | |
$sqrt | |
$subtract | |
$trunc |
Array Expression Operators数组表达式运算符
$arrayElemAt | |
$arrayToObject | |
$concatArrays | |
$filter | |
$firstN | $firstN accumulator.$firstN 累加器不同。 |
$in | |
$indexOfArray | -1 .-1 。 |
$isArray | |
$lastN | $lastN accumulator.$lastN 累加器不同。 |
$map | |
$maxN | n largest values in an array. n 个值。$maxN accumulator.$maxN 累加器不同。 |
$minN | n smallest values in an array. $minN accumulator.$minN 累加器不同。 |
$objectToArray | |
$range | |
$reduce | |
$reverseArray | |
$size | |
$slice | |
$sortArray | |
$zip |
Bitwise Operators位运算符
$bitAnd | and operation on an array of int or long values. int 值或long 值数组执行按位和运算的结果。New in version 6.3. |
$bitNot | not operation on a single argument or an array that contains a single int or long value. int 或long 值的数组执行逐位非运算的结果。New in version 6.3. |
$bitOr | or operation on an array of int or long values. int 或long 值数组执行按位or 运算的结果。New in version 6.3. |
$bitXor | xor (exclusive or) operation on an array of int and long values. int 和long 值数组执行逐位xor (异或)运算的结果。New in version 6.3. |
Boolean Expression Operators布尔表达式运算符
Boolean expressions evaluate their argument expressions as booleans and return a boolean as the result.布尔表达式将其参数表达式计算为布尔值,并返回布尔值作为结果。
In addition to the 除了false
boolean value, Boolean expression evaluates as false
the following: null
, 0
, and undefined
values. false
布尔值之外,布尔表达式还将以下值计算为false
:null
、0
和undefined
值。The Boolean expression evaluates all other values as 布尔表达式将所有其他值计算为true
, including non-zero numeric values and arrays.true
,包括非零数值和数组。
$and | true only when all its expressions evaluate to true . true 时才返回true 。 |
$not | |
$or | true when any of its expressions evaluates to true . true 时,返回true 。 |
Comparison Expression Operators比较表达式运算符
Comparison expressions return a boolean except for 比较表达式返回一个布尔值,但$cmp
which returns a number.$cmp
返回一个数字。
The comparison expressions take two argument expressions and compare both value and type, using the specified BSON comparison order for values of different types.比较表达式采用两个参数表达式,并对不同类型的值使用指定的BSON比较顺序来比较值和类型。
$cmp | 0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second.0 ;如果第一个值大于第二个值,则返回1 ;如果第一值小于第二个,则返回-1 。 |
$eq | true if the values are equivalent.true 。 |
$gt | true if the first value is greater than the second.true 。 |
$gte | true if the first value is greater than or equal to the second.true 。 |
$lt | true if the first value is less than the second.true 。 |
$lte | true if the first value is less than or equal to the second.true 。 |
$ne | true if the values are not equivalent.true 。 |
Conditional Expression Operators条件表达式运算符
$cond | |
$ifNull | null 结果,如果第一个表达式导致null 结果,则返回第二个表达式的结果。Null 结果包含未定义值或缺少字段的实例。null 。 |
$switch | true , $switch executes a specified expression and breaks out of the control flow.true 的表达式时,$switch 执行指定的表达式并脱离控制流。 |
Custom Aggregation Expression Operators自定义聚合表达式运算符
$accumulator | New in version 4.4. |
$function | New in version 4.4. |
Data Size Operators数据大小运算符
The following operators return the size of a data element:以下运算符返回数据元素的大小:
$binarySize | |
$bsonSize | Object ) when encoded as BSON. |
Date Expression Operators日期表达式运算符
The following operators returns date objects or components of a date object:以下运算符返回日期对象或日期对象的组件:
$dateAdd | |
$dateDiff | |
$dateFromParts | |
$dateFromString | |
$dateSubtract | |
$dateToParts | |
$dateToString | |
$dateTrunc | |
$dayOfMonth | |
$dayOfWeek | |
$dayOfYear | |
$hour | |
$isoDayOfWeek | 1 (for Monday) to 7 (for Sunday).1 (周一)到7 (周日)。 |
$isoWeek | 1 to 53 . 1 到53 。1 with the week (Monday through Sunday) that contains the year's first Thursday.1 开始,包含一年中第一个星期四的一周(星期一到星期日)。 |
$isoWeekYear | |
$millisecond | |
$minute | |
$month | |
$second | |
$toDate | New in version 4.0. |
$week | |
$year |
The following arithmetic operators can take date operands:以下算术运算符可以接受日期操作数:
$add | |
$subtract |
Literal Expression Operator文字表达式运算符
$literal | $literal expression to a string that starts with a dollar sign ($ ) to avoid parsing as a field path.$ )开头的字符串使用$literal 表达式,以避免将其解析为字段路径。 |
Miscellaneous Operators混杂运算符
$getField | $getField to retrieve the value of fields with names that contain periods (. ) or start with dollar signs ($ ). $getField 来检索名称包含句点(. )或以美元符号($ )开头的字段的值。New in version 5.0. |
$rand | |
$sampleRate |
Object Expression Operators对象表达式运算符
$mergeObjects | |
$objectToArray | |
$setField | $setField to add, update, or remove fields with names that contain periods (. ) or start with dollar signs ($ ). $setField 添加、更新或删除名称包含句点(. )或以美元符号($ )开头的字段。New in version 5.0. |
Set Expression Operators设置表达式运算符
Set expressions performs set operation on arrays, treating arrays as sets. 集合表达式对数组执行集合运算,将数组视为集合。Set expressions ignores the duplicate entries in each input array and the order of the elements.Set表达式会忽略每个输入数组中的重复项和元素的顺序。
If the set operation returns a set, the operation filters out duplicates in the result to output an array that contains only unique entries. 如果set操作返回一个集合,则该操作会筛选出结果中的重复项,以输出仅包含唯一项的数组。The order of the elements in the output array is unspecified.输出数组中元素的顺序未指定。
If a set contains a nested array element, the set expression does not descend into the nested array but evaluates the array at top-level.如果集合包含嵌套数组元素,则集合表达式不会下降到嵌套数组中,而是在顶级计算数组。
$allElementsTrue | true if no element of a set evaluates to false , otherwise, returns false . false ,则返回true ,否则返回false 。 |
$anyElementTrue | true if any elements of a set evaluate to true ; otherwise, returns false . true ,则返回true ;否则,返回false 。 |
$setDifference | |
$setEquals | true if the input sets have the same distinct elements. true 。 |
$setIntersection | |
$setIsSubset | true if all elements of the first set appear in the second set, including when the first set equals the second set; i.e. not a strict subset. true ;即不是严格的子集。 |
$setUnion |
String Expression Operators字符串表达式运算符
String expressions, with the exception of 字符串表达式($concat
, only have a well-defined behavior for strings of ASCII characters.$concat
除外)只对ASCII字符字符串具有定义良好的行为。
无论使用什么字符,$concat
behavior is well-defined regardless of the characters used.$concat
行为都是定义良好的。
$concat | |
$dateFromString | |
$dateToString | |
$indexOfBytes | -1 .-1 。 |
$indexOfCP | -1 -1 |
$ltrim | New in version 4.0. |
$regexFind | New in version 4.2. |
$regexFindAll | New in version 4.2. |
$regexMatch | New in version 4.2. |
$replaceOne | New in version 4.4. |
$replaceAll | New in version 4.4. |
$rtrim | New in version 4.0. |
$split | |
$strLenBytes | |
$strLenCP | |
$strcasecmp | 0 if two strings are equivalent, 1 if the first string is greater than the second, and -1 if the first string is less than the second.0 ;如果第一个字符串大于第二个字符串,则返回1 ;如果第一条字符串小于第二个,则返回-1 。 |
$substr | $substrBytes or $substrCP .$substrBytes 或$substrCP 。 |
$substrBytes | |
$substrCP | |
$toLower | |
$toString | New in version 4.0. |
$trim | New in version 4.0. |
$toUpper |
Text Expression Operator文本表达式运算符
$meta |
Timestamp Expression Operators时间戳表达式运算符
Timestamp expression operators return values from a timestamp.时间戳表达式运算符从时间戳返回值。
$tsIncrement | long . long 形式返回时间戳中的递增序号。New in version 5.1. |
$tsSecond | long .long 的形式返回返回。 New in version 5.1. |
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
to convert between degree and radian measurements.$degreesToRadians
和$radiansToDegrees
在度数和弧度测量之间转换。
$sin | |
$cos | |
$tan | |
$asin | |
$acos | |
$atan | |
$atan2 | y / x in radians, where y and x are the first and second values passed to the expression respectively.y/x 的反正切(反正切),其中y 和x 分别是传递给表达式的第一个值和第二个值。 |
$asinh | |
$acosh | |
$atanh | |
$sinh | |
$cosh | |
$tanh | |
$degreesToRadians | |
$radiansToDegrees |
Type Expression Operators类型表达式运算符
$convert | New in version 4.0. |
$isNumber | true if the specified expression resolves to an integer , decimal , double , or long .integer 、decimal 、double 或long ,则返回布尔值true 。false if the expression resolves to any other BSON type, null , or a missing field. null 或缺少字段,则返回布尔值false 。New in version 4.4. |
$toBool | New in version 4.0. |
$toDate | New in version 4.0. |
$toDecimal | New in version 4.0. |
$toDouble | New in version 4.0. |
$toInt | New in version 4.0. |
$toLong | New in version 4.0. |
$toObjectId | New in version 4.0. |
$toString | New in version 4.0. |
$type |
Accumulators累加器 ($group, $bucket, $bucketAuto, $setWindowFields
)
Aggregation accumulator operators:聚合累加器运算符:
-
Maintain their state as documents progress through the aggregation pipeline.在文档通过聚合管道时维护它们的状态。 -
Return totals, maxima, minima, and other values.返回总计、最大值、最小值和其他值。 -
Can be used in these aggregation pipeline stages:可用于以下聚合管道阶段:-
$setWindowFields
starting in MongoDB 5.0 (except when you are using the从MongoDB 5.0开始(除非使用$accumulator
or$mergeObjects
operators, which cannot be used with$setWindowFields
)$accumulator
或$mergeObjects
运算符,它们不能与$setWindowFields
一起使用)
Changed in version 5.0.
$accumulator | |
$addToSet | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$avg | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$bottom | New in version 5.2. $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
$bottomN | n fields within a group, according to the specified sort order. n 个字段的聚合。New in version 5.2. $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
$count | $count pipeline stage. $count 管道阶段不同。New in version 5.0 $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
$first | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$firstN | n elements within a group. n 个元素的聚合。$firstN array operator. $firstN 数组运算符不同。New in version 5.2 $group , expression and $setWindowFields stages. $group 、表达式和$setWindowFields 阶段中可用。 |
$last | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$lastN | n elements within a group. n 个元素的聚合。$lastN array operator. $lastN 数组运算符不同。New in version 5.2 $group , expression and $setWindowFields stages. $group 、表达式和$setWindowFields 阶段中可用。 |
$max | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$maxN | n maximum valued elements in a group. n 个最大值元素的聚合。$maxN array operator. $maxN 数组运算符不同。New in version 5.2. $group , $setWindowFields and as an expression. $group 、$setWindowFields 可用,并用作表达式。 |
$mergeObjects | |
$min | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$push | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$stdDevPop | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$stdDevSamp | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$sum | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$top | New in version 5.2. $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
$topN | n fields within a group, according to the specified sort order. n 个字段的聚合。New in version 5.2. $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
Accumulators (in Other Stages)累加器(其他阶段)
Some operators that are available as accumulators for the 一些可作为$group
stage are also available for use in other stages but not as accumulators. $group
阶段的累加器的运算符也可用于其他阶段,但不能用作累加器。When used in these other stages, these operators do not maintain their state and can take as input either a single argument or multiple arguments. For details, refer to the specific operator page.当在这些其他阶段中使用时,这些运算符不维护其状态,可以将单个参数或多个参数作为输入。有关详细信息,请参阅特定的运算符页面。
Changed in version 5.0.
The following accumulator operators are also available in the 以下累加器运算符也可用于$project
, $addFields
, $set
, and, starting in MongoDB 5.0, the $setWindowFields
stages.$project
、$addFields
、$set
,以及从MongoDB 5.0开始的$setWindowFields
阶段。
$avg | |
$first | |
$last | |
$max | |
$min | |
$stdDevPop | |
$stdDevSamp | |
$sum |
Variable Expression Operators变量表达式运算符
$let |
Window Operators窗口运算符
New in version 5.0.
Window operators return values from a defined span of documents from a collection, known as a window. 窗口运算符从集合(称为窗口)中定义的文档范围中返回值。A window is defined in the 窗口是在$setWindowFields
stage, available starting in MongoDB 5.0.$setWindowFields
阶段定义的,从MongoDB 5.0开始就可以使用。
The following window operators are available in the 以下窗口运算符在$setWindowFields
stage.$setWindowFields
阶段中可用。
$addToSet | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$avg | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$bottom | New in version 5.2. $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
$bottomN | n fields within a group, according to the specified sort order. n 个字段的聚合。New in version 5.2. $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
$count | $count pipeline stage. $count 管道阶段不同。New in version 5.0. |
$covariancePop | New in version 5.0. |
$covarianceSamp | New in version 5.0. |
$denseRank | $setWindowFields stage partition. $setWindowFields 阶段分区中其他文档的位置(称为排名)。New in version 5.0. |
$derivative | New in version 5.0. |
$documentNumber | $setWindowFields stage partition. $setWindowFields 阶段分区中的位置(称为文档编号)。New in version 5.0. |
$expMovingAvg | New in version 5.0. |
$first | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$integral | New in version 5.0. |
$last | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$linearFill | null and missing fields in a window using linear interpolation based on surrounding field values.null 字段和缺失字段。$setWindowFields stage. $setWindowFields 阶段中可用。New in version 5.3. |
$locf | null and missing fields in a window to the last non-null value for the field.null 和缺失字段的值设置为该字段的最后一个非null 值。$setWindowFields stage. $setWindowFields 阶段中可用。New in version 5.2. |
$max | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$min | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$minN | n minimum valued elements in a group. n 个最小值元素的聚合。$minN array operator. $minN 数组运算符不同。New in version 5.2. $group , $setWindowFields and as an expression. $group 、$setWindowFields 中可用,并用作表达式。 |
$push | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$rank | $setWindowFields stage partition.$setWindowFields 阶段分区中其他文档的位置(称为排名)。 New in version 5.0. |
$shift | $setWindowFields stage partition. $setWindowFields 阶段分区中相对于当前文档的指定位置。New in version 5.0. |
$stdDevPop | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$stdDevSamp | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$sum | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段中可用。 |
$top | New in version 5.2. $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
$topN | n fields within a group, according to the specified sort order. n 个字段的聚合。New in version 5.2. $group and $setWindowFields stages. $group 和$setWindowFields 阶段中可用。 |
Alphabetical Listing of Expression Operators表达式运算符的字母列表
$abs | |
$accumulator | |
$acos | |
$acosh | |
$add | |
$addToSet | Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$allElementsTrue | Returns true if no element of a set evaluates to false , otherwise, returns false . Accepts a single argument expression. |
$and | Returns true only when all its expressions evaluate to true . Accepts any number of argument expressions. |
$anyElementTrue | Returns true if any elements of a set evaluate to true ;
otherwise, returns false . Accepts a single argument expression. |
$arrayElemAt | Returns the element at the specified array index. |
$arrayToObject | Converts an array of key value pairs to a document. |
$asin | Returns the inverse sine (arc sine) of a value in radians. |
$asinh | Returns the inverse hyperbolic sin (hyperbolic arc sine) of a value in radians. |
$atan | Returns the inverse tangent (arc tangent) of a value in radians. |
$atan2 | Returns the inverse tangent (arc tangent) of y / x in radians, where y and x are the first and second values passed to the expression respectively. |
$atanh | Returns the inverse hyperbolic tangent (hyperbolic arc tangent) of a value in radians. |
$avg | Returns an average of numerical values. Ignores non-numeric values. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$binarySize | Returns the size of a given string or binary data value's content in bytes. New in version 4.4. |
$bitAnd | Returns the result of a bitwise and operation on an array of int or long values. New in version 6.3. |
$bitNot | Returns the result of a bitwise not operation on a single argument or an array that contains a single int or long value. New in version 6.3. |
$bitOr | Returns the result of a bitwise or operation on an array of int or long values. New in version 6.3. |
$bitXor | Returns the result of a bitwise xor (exclusive or) operation on an array of int and long values. New in version 6.3. |
$bottom | Returns the bottom element within a group according to the specified sort order. New in version 5.2. Available in $group and $setWindowFields stages. |
$bottomN | Returns an aggregation of the bottom n fields within a group, according to the specified sort order. New in version 5.2. Available in $group and $setWindowFields stages. |
$bsonSize | Returns the size in bytes of a given document (i.e. bsontype Object ) when encoded as BSON. New in version 4.4. |
$ceil | Returns the smallest integer greater than or equal to the specified number. |
$cmp | Returns: 0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second. |
$concat | Concatenates any number of strings. |
$concatArrays | Concatenates arrays to return the concatenated array. |
$cond | A ternary operator that evaluates one expression, and depending on the result, returns the value of one of the other two expressions. Accepts either three expressions in an ordered list or three named parameters. |
$convert | Converts a value to a specified type. |
$cos | Returns the cosine of a value that is measured in radians. |
$cosh | Returns the hyperbolic cosine of a value that is measured in radians. |
$count | Returns the number of documents in the group or window. New in version 5.0: Available in Distinct from the $group and $setWindowFields stages. $count pipeline stage. |
$covariancePop | Returns the population covariance of two numeric expressions. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$covarianceSamp | Returns the sample covariance of two numeric expressions. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$dateAdd | Adds a number of time units to a date object. |
$dateDiff | Returns the difference between two dates. |
$dateFromParts | Constructs a BSON Date object given the date's constituent parts. |
$dateSubtract | Subtracts a number of time units from a date object. |
$dateTrunc | Truncates a date. |
$dateToParts | Returns a document containing the constituent parts of a date. |
$dateFromString | Returns a date/time as a date object. |
$dateToString | Returns the date as a formatted string. |
$dayOfMonth | Returns the day of the month for a date as a number between 1 and 31. |
$dayOfWeek | Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday). |
$dayOfYear | Returns the day of the year for a date as a number between 1 and 366 (leap year). |
$degreesToRadians | Converts a value from degrees to radians. |
$denseRank | Returns the relative position of a sorted document. There are no gaps in the positions. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$derivative | Returns the average rate of change within the specified window. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$divide | Returns the result of dividing the first number by the second. Accepts two argument expressions. |
$documentNumber | Returns the position of a document (known as the document number) in the partition. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$eq | Returns true if the values are equivalent. |
$exp | Raises e to the specified exponent. |
$expMovingAvg | Returns the exponential moving average for the numeric expression. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$filter | Selects a subset of the array to return an array with only the elements that match the filter condition. |
$first | Returns a value from the first document for each group. Order is only defined if the documents are sorted. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$firstN | Returns a specified number of elements from the beginning of an array. Distinct from the $firstN accumulator. New in version 5.2. |
$firstN | Returns an aggregation of the first n fields within a group. Results are only meaningful when documents are in a defined order. Distinct from the $firstN array operator. New in version 5.2. Available in $group , expression and $setWindowFields stages. |
$floor | Returns the largest integer less than or equal to the specified number. |
$function | Defines a custom aggregation function. New in version 4.4. |
$getField | Returns the value of a specified field from a document. You can use $getField to retrieve the value of fields with names that contain periods (. ) or start with dollar signs ($ ). |
$gt | Returns true if the first value is greater than the second. |
$gte | Returns true if the first value is greater than or equal to the second. |
$hour | Returns the hour for a date as a number between 0 and 23. |
$ifNull | Returns either the non-null result of the first expression or the result of the second expression if the first expression results in a null result. Null result encompasses instances of undefined values or missing fields. Accepts two expressions as arguments. The result of the second expression can be null. |
$in | Returns a boolean indicating whether a specified value is in an array. |
$indexOfArray | Searches an array for an occurrence of a specified value and returns the array index of the first occurrence. If the substring is not found, returns -1 . |
$indexOfBytes | Searches a string for an occurrence of a substring and returns the UTF-8 byte index of the first occurrence. If the substring is not found, returns -1 . |
$indexOfCP | Searches a string for an occurrence of a substring and returns the UTF-8 code point index of the first occurrence. If the substring is not found, returns -1 . |
$integral | Returns the approximation of the area under a curve. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$isArray | Determines if the operand is an array. Returns a boolean. |
$isNumber | Determines if the expression resolves to an integer, double, decimal, or long. |
$isoDayOfWeek | Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday). |
$isoWeek | Returns the week number in ISO 8601 format, ranging from 1 to 53 . Week numbers start at 1 with the week (Monday through Sunday) that contains the year's first Thursday. |
$isoWeekYear | Returns the year number in ISO 8601 format. The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601). |
$last | Returns a value from the last document for each group. Order is only defined if the documents are sorted. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$lastN | Returns a specified number of elements from the end of an array. Distinct from the $lastN accumulator. New in version 5.2. |
$lastN | Returns an aggregation of the last n fields within a group. Results are only meaningful when documents are in a defined order. Distinct from the $lastN array operator. New in version 5.2. Available in $group , expression and $setWindowFields stages. |
$let | Defines variables for use within the scope of a subexpression and returns the result of the subexpression. Accepts named parameters. Accepts any number of argument expressions. |
$linearFill | Fills null and missing fields in a window using linear interpolation based on surrounding field values. New in version 5.3. |
$literal | Return a value without parsing. Use for values that the aggregation pipeline 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. |
$ln | Calculates the natural log of a number. |
$locf | Last observation carried forward. Sets values for null and missing fields in a window to the last non-null value for the field. New in version 5.2. |
$log | Calculates the log of a number in the specified base. |
$log10 | Calculates the log base 10 of a number. |
$lt | Returns true if the first value is less than the second. |
$lte | Returns true if the first value is less than or equal to the second. |
$ltrim | Removes whitespace or the specified characters from the beginning of a string. |
$map | Applies a subexpression to each element of an array and returns the array of resulting values in order. Accepts named parameters. |
$max | Returns the highest expression value for each group. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$maxN | Returns the n largest values in an array. Distinct from the $maxN accumulator. New in version 5.2. |
$maxN | Returns an aggregation of the n maximum valued elements in a group. Distinct from the $maxN array operator. |
$mergeObjects | Combines multiple documents into a single document. |
$meta | Access available per-document metadata related to the aggregation operation. |
$min | Returns the lowest expression value for each group. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$minN | Returns the n smallest values in an array. Distinct from the $minN accumulator. New in version 5.2. |
$minN | Returns an aggregation of the n minimum valued elements in a group. Distinct from the $minN array operator. |
$millisecond | Returns the milliseconds of a date as a number between 0 and 999. |
$minute | Returns the minute for a date as a number between 0 and 59. |
$mod | Returns the remainder of the first number divided by the second. Accepts two argument expressions. |
$month | Returns the month for a date as a number between 1 (January) and 12 (December). |
$multiply | Multiplies numbers to return the product. Accepts any number of argument expressions. |
$ne | Returns true if the values are not equivalent. |
$not | Returns the boolean value that is the opposite of its argument expression. Accepts a single argument expression. |
$objectToArray | Converts a document to an array of documents representing key-value pairs. |
$or | Returns true when any of its expressions evaluates to true . Accepts any number of argument expressions. |
$pow | Raises a number to the specified exponent. |
$push | Returns an array of expression values for documents in each group. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$radiansToDegrees | Converts a value from radians to degrees. |
$rand | Returns a random float between 0 and 1. |
$range | Outputs an array containing a sequence of integers according to user-defined inputs. |
$rank | Returns the document position (known as the rank) relative to other sorted documents. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$reduce | Applies an expression to each element in an array and combines them into a single value. |
$regexFind | Applies a regular expression (regex) to a string and returns information on the first matched substring. |
$regexFindAll | Applies a regular expression (regex) to a string and returns information on the all matched substrings. |
$regexMatch | Applies a regular expression (regex) to a string and returns a boolean that indicates if a match is found or not. |
$replaceOne | Replaces the first instance of a matched string in a given input. New in version 4.4. |
$replaceAll | Replaces all instances of a matched string in a given input. New in version 4.4. |
$reverseArray | Returns an array with the elements in reverse order. |
$round | Rounds a number to a whole integer or to a specified decimal place. |
$rtrim | Removes whitespace or the specified characters from the end of a string. |
$sampleRate | Randomly select documents at a given rate. Although the exact number of documents selected varies on each run, the quantity chosen approximates the sample rate expressed as a percentage of the total number of documents. |
$second | Returns the seconds for a date as a number between 0 and 60 (leap seconds). |
$setDifference | Returns a set with elements that appear in the first set but not in the second set; i.e. performs a relative complement of the second set relative to the first. Accepts exactly two argument expressions. |
$setEquals | Returns true if the input sets have the same distinct elements. Accepts two or more argument expressions. |
$setField | Adds, updates, or removes a specified field in a document. You can use $setField to add, update, or remove fields with names that contain periods (. ) or start with dollar signs ($ ). New in version 5.0. |
$setIntersection | Returns a set with elements that appear in all of the input sets. Accepts any number of argument expressions. |
$setIsSubset | Returns true if all elements of the first set appear in the second set, including when the first set equals the second set; i.e. not a strict subset. Accepts exactly two argument expressions. |
$setUnion | Returns a set with elements that appear in any of the input sets. |
$shift | Returns the value from an expression applied to a document in a specified position relative to the current document in the output. New in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$size | Returns the number of elements in the array. Accepts a single expression as argument. |
$sin | Returns the sine of a value that is measured in radians. |
$sinh | Returns the hyperbolic sine of a value that is measured in radians. |
$slice | Returns a subset of an array. |
$sortArray | Sorts an array based on its elements. |
$split | Splits a string into substrings based on a delimiter. Returns an array of substrings. If the delimiter is not found within the string, returns an array containing the original string. |
$sqrt | Calculates the square root. |
$stdDevPop | Returns the population standard deviation of the input values. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$stdDevSamp | Returns the sample standard deviation of the input values. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$strcasecmp | Performs case-insensitive string comparison and returns: 0 if two strings are equivalent, 1 if the first string is greater than the second, and -1 if the first string is less than the second. |
$strLenBytes | Returns the number of UTF-8 encoded bytes in a string. |
$strLenCP | Returns the number of UTF-8 code points in a string. |
$substr | Deprecated. Use $substrBytes or $substrCP . |
$substrBytes | Returns the substring of a string. Starts with the character at the specified UTF-8 byte index (zero-based) in the string and continues for the specified number of bytes. |
$substrCP | Returns the substring of a string. Starts with the character at the specified UTF-8 code point (CP) index (zero-based) in the string and continues for the number of code points specified. |
$subtract | Returns the result of subtracting the second value from the first. If the two values are numbers, return the difference. If the two values are dates, return the difference in milliseconds. If the two values are a date and a number in milliseconds, return the resulting date. Accepts two argument expressions. If the two values are a date and a number, specify the date argument first as it is not meaningful to subtract a date from a number. |
$sum | Returns a sum of numerical values. Ignores non-numeric values. Changed in version 5.0 $setWindowFields stage. $setWindowFields 阶段可用。 |
$switch | Evaluates a series of case expressions. When it finds an expression which evaluates to true , $switch executes a specified expression and breaks out of the control flow. |
$tan | Returns the tangent of a value that is measured in radians. |
$tanh | Returns the hyperbolic tangent of a value that is measured in radians. |
$toBool | Converts value to a boolean. |
$toDate | Converts value to a Date. |
$toDecimal | Converts value to a Decimal128. |
$toDouble | Converts value to a double. |
$toInt | Converts value to an integer. |
$toLong | Converts value to a long. |
$toObjectId | Converts value to an ObjectId. |
$top | Returns the top element within a group according to the specified sort order. New in version 5.2. Available in $group and $setWindowFields stages. |
$topN | Returns an aggregation of the top n fields within a group, according to the specified sort order. New in version 5.2. Available in $group and $setWindowFields stages. |
$toString | Converts value to a string. |
$toLower | Converts a string to lowercase. Accepts a single argument expression. |
$toUpper | Converts a string to uppercase. Accepts a single argument expression. |
$trim | Removes whitespace or the specified characters from the beginning and end of a string. |
$trunc | Truncates a number to a whole integer or to a specified decimal place. |
$tsIncrement | Returns the incrementing ordinal from a timestamp as a long . New in version 5.1. |
$tsSecond | Returns the seconds from a timestamp as a long . New in version 5.1. |
$type | Return the BSON data type of the field. |
$unsetField | Removes a specified field from a document. An alias for $setField to remove fields with names that contain periods (. ) or that start with dollar signs ($ ). New in version 5.0. |
$week | Returns the week number for a date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year). |
$year | Returns the year for a date as a number (e.g. 2014). |
$zip | Merge two arrays together. |
For the pipeline stages, see Aggregation Pipeline Stages.