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 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.一些算术表达式也可以支持日期算术。

Name名称Description描述
$absReturns the absolute value of a number.返回数字的绝对值。
$addAdds numbers to return the sum, or adds numbers and a date to return a new date. 添加数字以返回总和,或添加数字和日期以返回新日期。If adding numbers and a date, treats the numbers as milliseconds. 如果添加数字和日期,则将这些数字视为毫秒。
Accepts any number of argument expressions, but at most, one expression can resolve to a date.接受任意数量的参数表达式,但最多一个表达式可以解析为一个日期。
$ceilReturns the smallest integer greater than or equal to the specified number.返回大于或等于指定数字的最小整数。
$divideReturns the result of dividing the first number by the second. Accepts two argument expressions.返回第一个数除以第二个数的结果。接受两个参数表达式。
$expRaises e to the specified exponent.e幂乘到指定的指数。
$floorReturns the largest integer less than or equal to the specified 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.计算一个数字的对数基数10。
$modReturns the remainder of the first number divided by the second. Accepts two argument expressions.返回第一个数除以第二个数的余数。接受两个参数表达式。
$multiplyMultiplies numbers to return the product. 将数字相乘以返回产品。Accepts any number of argument expressions.接受任意数量的参数表达式。
$powRaises a number to the specified exponent.将数字幂乘到指定的指数。
$roundRounds a number to to a whole integer or to a specified decimal place.将数字舍入为整数或指定的小数点。
$sqrtCalculates the square root.计算平方根。
$subtractReturns 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.如果这两个值是日期和数字,请首先指定日期参数,因为从数字中减去日期没有意义。
$truncTruncates a number to a whole integer or to a specified decimal place.将数字截断为整数指定的小数位。

Array Expression Operators数组表达式运算符

Name名称Description描述
$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 only the elements that match the filter condition.选择数组的子集以返回仅包含与筛选条件匹配的元素的数组。
$firstReturns the first array element. 返回第一个数组元素。Distinct from $first accumulator.有别于$first累加器。
$firstNReturns a specified number of elements from the beginning of an array. 从数组的开头返回指定数量的元素。Distinct from the $firstN accumulator.有别于$firstN累加器。
$inReturns a boolean indicating whether a specified value is in an array.返回一个布尔值,指示指定的值是否在数组中。
$indexOfArraySearches 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.如果未找到子字符串,则返回-1
$isArrayDetermines if the operand is an array. 确定操作数是否为数组。Returns a boolean.返回一个布尔值。
$lastReturns the last array element. 返回最后一个数组元素。Distinct from $last accumulator.有别于$last累加器。
$lastNReturns a specified number of elements from the end of an array. 从数组末尾返回指定数量的元素。Distinct from the $lastN accumulator.有别于$lastN累加器。
$mapApplies a subexpression to each element of an array and returns the array of resulting values in order. 将子表达式应用于数组的每个元素,并按顺序返回结果值数组。Accepts named parameters.接受命名参数。
$maxNReturns the n largest values in an array. 返回数组中的n个最大值。Distinct from the $maxN accumulator.有别于$maxN累加器。
$minNReturns the n smallest values in an array. 返回数组中的n个最小值。Distinct from the $minN accumulator.有别于$minN累加器。
$objectToArrayConverts a document to an array of documents representing key-value pairs.将文档转换为表示键值对的文档数组。
$rangeOutputs an array containing a sequence of integers according to user-defined inputs.根据用户定义的输入输出包含整数序列的数组。
$reduceApplies an expression to each element in an array and combines them into a single value.将表达式应用于数组中的每个元素,并将它们组合为单个值。
$reverseArrayReturns an array with the elements in reverse order.返回元素顺序相反的数组。
$sizeReturns the number of elements in the array. 返回数组中的元素数。Accepts a single expression as argument.接受单个表达式作为参数。
$sliceReturns a subset of an array.返回数组的子集。
$sortArraySorts the elements of an array.对数组中的元素进行排序。
$zipMerge two arrays together.将两个数组合并在一起。

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布尔值外,以下值:null0undefined的布尔表达式的计算结果为falseThe Boolean expression evaluates all other values as true, including non-zero numeric values and arrays.布尔表达式将所有其他值计算为true,包括非零数值和数组。

Name名称Description描述
$andReturns true only when all its expressions evaluate to true. 仅当其所有表达式的计算结果均为true时,才返回trueAccepts any number of argument expressions.接受任意数量的参数表达式。
$notReturns the boolean value that is the opposite of its argument expression. 返回与其参数表达式相反的布尔值。Accepts a single argument expression.接受单个参数表达式。
$orReturns true when any of its expressions evaluates to true. 当其任何表达式的计算结果为true时,返回trueAccepts any number of argument expressions.接受任意数量的参数表达式。

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比较顺序

Name名称Description描述
$cmpReturns 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
$eqReturns true if the values are equivalent.如果值相等,则返回true
$gtReturns true if the first value is greater than the second.如果第一个值大于第二个值,则返回true
$gteReturns true if the first value is greater than or equal to the second.如果第一个值大于或等于第二个值,则返回true
$ltReturns true if the first value is less than the second.如果第一个值小于第二个值,则返回true
$lteReturns true if the first value is less than or equal to the second.如果第一个值小于或等于第二个值,则返回true
$neReturns true if the values are not equivalent.如果值不相等,则返回true

Conditional Expression Operators条件表达式运算符

Name名称Description描述
$condA 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.接受有序列表中的三个表达式或三个命名参数。
$ifNullReturns 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.接受两个表达式作为参数。第二个表达式的结果可以为null
$switchEvaluates 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.当它找到一个计算结果为true的表达式时,$switch执行一个指定的表达式并中断控制流。

Custom Aggregation Expression Operators自定义聚合表达式运算符

Name名称Description描述
$accumulator

Defines a custom accumulator function.定义自定义累加器函数。

New in version 4.4.在版本4.4中新增

$function

Defines a custom function.定义一个自定义函数。

New in version 4.4.在版本4.4中新增

Data Size Operators数据大小运算符

The following operators return the size of a data element:以下运算符返回数据元素的大小:

Name名称Description描述
$binarySizeReturns the size of a given string or binary data value's content in bytes.返回给定字符串或二进制数据值内容的大小(字节)。
$bsonSizeReturns the size in bytes of a given document (i.e. bsontype Object) when encoded as BSON.当编码为BSON时,返回给定文档(即bsontype Object)的字节大小。

Date Expression Operators日期表达式运算符

The following operators returns date objects or components of a date object:以下运算符返回日期对象或日期对象的组件:

Name名称Description描述
$dateAddAdds a number of time units to a date object.向日期对象添加多个时间单位。
$dateDiffReturns the difference between two dates.返回两个日期之间的差值。
$dateFromPartsConstructs a BSON Date object given the date's constituent parts.根据日期的组成部分构造BSON Date对象。
$dateFromStringConverts a date/time string to a date object.将日期/时间字符串转换为日期对象。
$dateSubtractSubtracts a number of time units from a date object.从日期对象中减去若干时间单位。
$dateToPartsReturns a document containing the constituent parts of a date.返回包含日期组成部分的文档。
$dateToStringReturns the date as a formatted string.以格式化字符串的形式返回日期。
$dateTruncTruncates a date.截断日期。
$dayOfMonthReturns the day of the month for a date as a number between 1 and 31.以1到31之间的数字返回日期的月份。
$dayOfWeekReturns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).以1(星期日)到7(星期六)之间的数字形式返回日期的星期几。
$dayOfYearReturns the day of the year for a date as a number between 1 and 366 (leap year).以介于1和366(闰年)之间的数字返回日期的一年中的某一天。
$hourReturns the hour for a date as a number between 0 and 23.以0到23之间的数字返回日期的小时。
$isoDayOfWeekReturns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).以ISO 8601格式返回工作日编号,范围从1(周一)到7(周日)。
$isoWeekReturns the week number in ISO 8601 format, ranging from 1 to 53. 返回ISO 8601格式的周数,范围从153Week numbers start at 1 with the week (Monday through Sunday) that contains the year's first Thursday.周数从1开始,以包含一年中第一个星期四的一周(周一到周日)为单位。
$isoWeekYearReturns the year number in ISO 8601 format. 返回ISO 8601格式的年份号。The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601).一年从第一周的星期一开始(ISO 8601),到最后一周的星期日结束(ISO 8601)。
$millisecondReturns the milliseconds of a date as a number between 0 and 999.以0到999之间的数字形式返回日期的毫秒数。
$minuteReturns the minute for a date as a number between 0 and 59.以0到59之间的数字返回日期的分钟。
$monthReturns the month for a date as a number between 1 (January) and 12 (December).将日期的月份返回为介于1(1月)和12(12月)之间的数字。
$secondReturns the seconds for a date as a number between 0 and 60 (leap seconds).以0到60之间的数字(闰秒)返回日期的秒数。
$toDate

Converts value to a Date.将值转换为日期。

New in version 4.0.在版本4.0中新增

$weekReturns 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).将日期的周数返回为0(一年中第一个星期日之前的部分周)和53(闰年)之间的数字。
$yearReturns the year for a date as a number (e.g. 2014).以数字形式返回日期的年份(例如2014年)。

The following arithmetic operators can take date operands:以下算术运算符可以采用日期操作数:

Name名称Description描述
$addAdds numbers and a date to return a new date. 添加数字和日期以返回新日期。If adding numbers and a date, treats the numbers as milliseconds. 如果添加数字和日期,则将这些数字视为毫秒。Accepts any number of argument expressions, but at most, one expression can resolve to a date.接受任意数量的参数表达式,但最多一个表达式可以解析为一个日期。
$subtractReturns the result of subtracting the second value from the first. 返回从第一个值中减去第二个值的结果。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.如果这两个值是日期和数字,请首先指定日期参数,因为从数字中减去日期没有意义。

Literal Expression Operator文字表达式运算符

Name名称Description描述
$literalReturn 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 dollar sign ($) to avoid parsing as a field path.例如,对以美元符号($)开头的字符串使用$literal表达式,以避免解析为字段路径。

Miscellaneous Operators混杂运算符

Name名称Description描述
$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 ($).可以使用$getField检索名称中包含句点(.)或者以美元符号($)开始的字段的值。

New in version 5.0.在版本5.0中新增

$randReturns a random float between 0 and 1返回介于0和1之间的随机浮点值
$sampleRateRandomly 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.虽然每次运行时选择的文档的确切数量各不相同,但选择的数量近似于样本率,以文档总数的百分比表示。

Object Expression Operators对象表达式运算符

Name名称Description描述
$mergeObjectsCombines multiple documents into a single document.将多个文档合并为一个文档。
$objectToArrayConverts a document to an array of documents representing key-value pairs.将文档转换为表示键值对的文档数组。
$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 ($).可以使用$setField添加、更新或删除名称中包含句点(.)或者以美元符号($)开始的字段。

New in version 5.0.在版本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.集合表达式会忽略每个输入数组中的重复项以及元素的顺序。

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操作返回一个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.如果集合包含嵌套数组元素,集合表达式不会下降到嵌套数组中,而是在顶层计算数组。

Name名称Description描述
$allElementsTrueReturns true if no element of a set evaluates to false, otherwise, returns false. 如果集合中没有元素的计算结果为false,则返回true,否则返回falseAccepts a single argument expression.接受单个参数表达式。
$anyElementTrueReturns true if any elements of a set evaluate to true; otherwise, returns false. 如果集合中的任何元素的计算结果为true,则返回true;否则,返回falseAccepts a single argument expression.接受单个参数表达式。
$setDifferenceReturns 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.只接受两个参数表达式。
$setEqualsReturns true if the input sets have the same distinct elements. 如果输入集具有相同的不同元素,则返回trueAccepts 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 the second set, including when the first set equals the second set; i.e. not a strict subset. 如果第一个集合的所有元素都出现在第二个集合中,包括第一个集合等于第二个集合时;也就是说,不是严格的子集,则返回trueAccepts exactly two argument expressions.只接受两个参数表达式。
$setUnionReturns a set with elements that appear in any of the input sets.返回包含出现在任何输入集中的元素的集合。

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的行为都是明确定义的。

Name名称Description描述
$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 occurrence of a substring and returns the UTF-8 byte index of the first occurrence. 在字符串中搜索子字符串的匹配项,并返回第一个匹配项的UTF-8字节索引。If the substring is not found, returns -1.如果未找到子字符串,则返回-1
$indexOfCPSearches a string for an occurrence of a substring and returns the UTF-8 code point index of the first occurrence. 在字符串中搜索子字符串的匹配项,并返回第一个匹配项的UTF-8代码点索引。If the substring is not found, returns -1如果未找到子字符串,则返回-1
$ltrim

Removes whitespace or the specified characters from the beginning of a string.删除字符串开头的空白或指定字符。

New in version 4.0.在版本4.0中新增

$regexFind

Applies a regular expression (regex) to a string and returns information on the first matched substring.将正则表达式(regex)应用于字符串,并返回有关第一个匹配子字符串的信息。

New in version 4.2.在版本4.2中新增

$regexFindAll

Applies a regular expression (regex) to a string and returns information on the all matched substrings.将正则表达式(regex)应用于字符串,并返回有关所有匹配子字符串的信息。

New in version 4.2.在版本4.2中新增

$regexMatch

Applies a regular expression (regex) to a string and returns a boolean that indicates if a match is found or not.将正则表达式(regex)应用于字符串,并返回一个布尔值,指示是否找到匹配项。

New in version 4.2.在版本4.2中新增

$replaceOne

Replaces the first instance of a matched string in a given input.替换给定输入中匹配字符串的第一个实例。

New in version 4.4.在版本4.4中新增

$replaceAll

Replaces all instances of a matched string in a given input.替换给定输入中匹配字符串的所有实例。

New in version 4.4.在版本4.4中新增

$rtrim

Removes whitespace or the specified characters from the end of a string.从字符串末尾删除空格或指定字符。

New in version 4.0.在版本4.0中新增

$splitSplits 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.如果在字符串中找不到分隔符,则返回包含原始字符串的数组。
$strLenBytesReturns the number of UTF-8 encoded bytes in a string.返回字符串中UTF-8编码的字节数。
$strLenCPReturns the number of UTF-8 code points in a string.返回字符串中UTF-8代码点的数目。
$strcasecmpPerforms 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.执行不区分大小写的字符串比较,如果两个字符串相等,则返回0;如果第一个字符串大于第二个字符串,则返回1;如果第一个字符串小于第二个字符串,则返回-1
$substrDeprecated. 不赞成。Use $substrBytes or $substrCP.请使用$substrBytes$substrCP
$substrBytesReturns 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.返回字符串的子字符串。从字符串中指定的UTF-8字节索引(从零开始)处的字符开始,并持续指定的字节数。
$substrCPReturns 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.返回字符串的子字符串。从字符串中指定的UTF-8代码点(CP)索引(从零开始)处的字符开始,并按指定的代码点数继续。
$toLowerConverts a string to lowercase. 将字符串转换为小写。Accepts a single argument expression.接受单个参数表达式。
$toString

Converts value to a string.将值转换为字符串。

New in version 4.0.在版本4.0中新增

$trim

Removes whitespace or the specified characters from the beginning and end of a string.删除字符串开头和结尾的空白或指定字符。

New in version 4.0.在版本4.0中新增

$toUpperConverts a string to uppercase. Accepts a single argument expression.将字符串转换为大写。接受单个参数表达式。

Text Expression Operator文本表达式运算符

Name名称Description描述
$metaAccess available per-document metadata related to the aggregation operation.可访问与聚合操作相关的每个文档元数据。

Timestamp Expression Operators时间戳表达式运算符

Timestamp expression operators return values from a timestamp.时间戳表达式运算符从时间戳返回值。

Name名称Description描述
$tsIncrement

Returns the incrementing ordinal from a timestamp as a long.时间戳中的递增序号以long的形式返回。

New in version 5.1.在版本5.1中新增

$tsSecond

Returns the seconds from a timestamp as a long.时间戳中的秒数以long的形式返回。

New in version 5.1.在版本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在度和弧度测量之间进行转换。

Name名称Description描述
$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 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 the expression respectively.返回y/x的逆切线(反正切),单位为弧度,其中yx分别是传递给表达式的第一个和第二个值。
$asinhReturns the inverse hyperbolic sine (hyperbolic arc sine) of a value in radians.返回以弧度为单位的值的反双曲正弦(双曲弧正弦)。
$acoshReturns the inverse hyperbolic cosine (hyperbolic arc cosine) of a value in radians.返回以弧度为单位的值的反双曲余弦(双曲弧余弦)。
$atanhReturns the inverse hyperbolic tangent (hyperbolic arc tangent) of a value in radians.返回以弧度为单位的值的反双曲正切(双曲反正切)。
$sinhReturns the hyperbolic sine of a value that is measured in radians.返回以弧度度量的值的双曲正弦。
$coshReturns the hyperbolic cosine of a value that is measured in radians.返回以弧度度量的值的双曲余弦。
$tanhReturns the hyperbolic tangent of a value that is measured in radians.返回以弧度度量的值的双曲正切。
$degreesToRadiansConverts a value from degrees to radians.将值从度转换为弧度。
$radiansToDegreesConverts a value from radians to degrees.将值从弧度转换为度。

Type Expression Operators类型表达式运算符

Name名称Description描述
$convert

Converts a value to a specified type.将值转换为指定的类型。

New in version 4.0.在版本4.0中新增

$isNumber

Returns boolean true if the specified expression resolves to an integer, decimal, double, or long.如果指定的表达式解析为integerdecimaldoublelong,则返回布尔值true

Returns boolean false if the expression resolves to any other BSON type, null, or a missing field.如果表达式解析为任何其他BSON类型null或缺少的字段,则返回布尔值false

New in version 4.4.在版本4.4中新增

$toBool

Converts value to a boolean.将值转换为布尔值。

New in version 4.0.在版本4.0中新增

$toDate

Converts value to a Date.将值转换为日期。

New in version 4.0.在版本4.0中新增

$toDecimal

Converts value to a Decimal128.将值转换为小数128。

New in version 4.0.在版本4.0中新增

$toDouble

Converts value to a double.将值转换为双精度。

New in version 4.0.在版本4.0中新增

$toInt

Converts value to an integer.将值转换为整数。

New in version 4.0.在版本4.0中新增

$toLong

Converts value to a long.将值转换为长整型。

New in version 4.0.在版本4.0中新增

$toObjectId

Converts value to an ObjectId.将值转换为ObjectId。

New in version 4.0.在版本4.0中新增

$toString

Converts value to a string.将值转换为字符串。

New in version 4.0.在版本4.0中新增

$typeReturn the BSON data type of the field.返回字段的BSON数据类型。

Accumulators累加器 ($group, $bucket, $bucketAuto, $setWindowFields)

Aggregation accumulator operators:聚合累加器运算符:

Changed in version 5.0.在版本5.0中更改

Name名称Description描述
$accumulatorReturns the result of a user-defined accumulator function.返回用户定义的累加器函数的结果。
$addToSet

Returns an array of unique expression values for each group. Order of the array elements is undefined.返回每个组的唯一表达式值数组。数组元素的顺序未定义。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$avg

Returns an average of numerical values. 返回数值的平均值。Ignores non-numeric values.忽略非数值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$bottom

Returns the bottom element within a group according to the specified sort order.根据指定的排序顺序返回组中的底部元素。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$bottomN

Returns an aggregation of the bottom n fields within a group, according to the specified sort order.根据指定的排序顺序,返回组中底部n个字段的聚合。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$count

Returns the number of documents in a group.返回组中的文档数。

Distinct from the $count pipeline stage.有别于$count管道阶段。

New in version 5.0.在版本5.0中新增 Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$first

Returns a value from the first document for each group. 从每个组的第一个文档返回一个值。Order is only defined if the documents are sorted.仅当文档已排序时,才定义顺序。

Distinct from the $first array operator.有别于$first数组运管符。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$firstN

Returns an aggregation of the first n elements within a group. 返回组中前n个元素的聚合。Only meaningful when documents are in a defined order. 仅当文档按定义的顺序排列时才有意义。Distinct from the $firstN array operator.$firstN数组运算符不同。

New in version 5.2.在版本5.2中新增Available in $group, expression and $setWindowFields stages.$group阶段、表达式$setWindowFields阶段中可用。

$last

Returns a value from the last document for each group. 从每个组的最后一个文档中返回一个值。Order is only defined if the documents are sorted.仅当文档已排序时,才定义顺序。

Distinct from the $last array operator.有别于$last数组运算符。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$lastN

Returns an aggregation of the last n elements within a group. 返回组中最后n个元素的聚合。Only meaningful when documents are in a defined order. 仅当文档按定义的顺序排列时才有意义。Distinct from the $lastN array operator.有别于$lastN数组运算符。

New in version 5.2.在版本5.2中新增Available in $group, expression and $setWindowFields stages.$group阶段、表达式$setWindowFields阶段中可用。

$max

Returns the highest expression value for each group.返回每个组的最高表达式值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$maxN

Returns an aggregation of the n maximum valued elements in a group. 返回组中n个最大值元素的聚合。Distinct from the $maxN array operator.$maxN数组运算符不同。

New in version 5.2.在版本5.2中新增

Available in $group, $setWindowFields and as an expression.$group阶段和$setWindowFields阶段中可用,并用作表达式

$mergeObjectsReturns a document created by combining the input documents for each group.返回通过组合每个组的输入文档创建的文档。
$min

Returns the lowest expression value for each group.返回每个组的最低表达式值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$push

Returns an array of expression values for documents in each group.返回每个组中文档的表达式值数组。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$stdDevPop

Returns the population standard deviation of the input values.返回输入值的总体标准偏差。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$stdDevSamp

Returns the sample standard deviation of the input values.返回输入值的样本标准偏差。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$sum

Returns a sum of numerical values. Ignores non-numeric values.返回数值之和。忽略非数值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$top

Returns the top element within a group according to the specified sort order.根据指定的排序顺序返回组中的顶部元素。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$topN

Returns an aggregation of the top n fields within a group, according to the specified sort order.根据指定的排序顺序,返回组中前n个字段的聚合。

New in version 5.2.在版本5.2中新增

Available in $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.在版本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阶段中可用。

Name名称Description描述
$avgReturns an average of the specified expression or list of expressions for each document. 返回每个文档的指定表达式或表达式列表的平均值。Ignores non-numeric values.忽略非数值。
$maxReturns the maximum of the specified expression or list of expressions for each document返回每个文档的指定表达式或表达式列表的最大值
$minReturns the minimum of the specified expression or list of expressions 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变量表达式运算符

Name名称Description描述
$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.接受任意数量的参数表达式。

Window Operators窗口运算符

New in version 5.0.在版本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阶段使用。

Name名称Description描述
$addToSet

Returns an array of all unique values that results from applying an expression to each document.返回将表达式应用于每个文档所产生的所有唯一值的数组。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$avg

Returns the average for the specified expression. 返回指定表达式的平均值。Ignores non-numeric values.忽略非数值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$bottom

Returns the bottom element within a group according to the specified sort order.根据指定的排序顺序返回组中的底部元素。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$bottomN

Returns an aggregation of the bottom n fields within a group, according to the specified sort order.根据指定的排序顺序,返回组中底部n个字段的聚合。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$count

Returns the number of documents in the group or window.返回组或窗口中的文档数。

Distinct from the $count pipeline stage.有别于$count管道阶段。

New in version 5.0.在版本5.0中新增

$covariancePop

Returns the population covariance of two numeric expressions.返回两个数值表达式的总体协方差。

New in version 5.0.在版本5.0中新增

$covarianceSamp

Returns the sample covariance of two numeric expressions.返回两个数值表达式的样本协方差。

New in version 5.0.在版本5.0中新增

$denseRank

Returns the document position (known as the rank) relative to other documents in the $setWindowFields stage partition. 返回相对于$setWindowFields阶段分区中其他文档的文档位置(称为排名)。There are no gaps in the ranks. 排名中没有间隙。Ties receive the same rank.并列者接受相同的排名。

New in version 5.0.在版本5.0中新增

$derivative

Returns the average rate of change within the specified window.返回指定窗口内的平均更改率。

New in version 5.0.在版本5.0中新增

$documentNumber

Returns the position of a document (known as the document number) in the $setWindowFields stage partition. 返回文档(称为文档编号)在$setWindowFields阶段分区中的位置。Ties result in different adjacent document numbers.并列者会导致不同的相邻文档编号。

New in version 5.0.在版本5.0中新增

$expMovingAvg

Returns the exponential moving average for the numeric expression.返回数值表达式的指数移动平均值。

New in version 5.0.在版本5.0中新增

$first

Returns the value that results from applying an expression to the first document in a group or window.返回将表达式应用于组或窗口中的第一个文档所产生的值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$integral

Returns the approximation of the area under a curve.返回曲线下面积的近似值。

New in version 5.0.在版本5.0中新增

$last

Returns the value that results from applying an expression to the last document in a group or window.返回将表达式应用于组或窗口中的最后一个文档所产生的值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$linearFill

Fills null and missing fields in a window using linear interpolation based on surrounding field values.使用基于周围字段值的线性插值填充窗口中的null字段和缺失字段。

Available in $setWindowFields stage.$setWindowFields阶段中可用。

New in version 5.3.在版本5.3中新增

$locf

Last observation carried forward. 最后的观察结果继续进行。Sets values for null and missing fields in a window to the last non-null value for the field.窗口null字段和缺失字段的值设置为该字段的最后一个非空值。

Available in $setWindowFields stage.$setWindowFields阶段中可用。

New in version 5.2.在版本5.2中新增

$max

Returns the maximum value that results from applying an expression to each document.返回对每个文档应用表达式所产生的最大值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$min

Returns the minimum value that results from applying an expression to each document.返回将表达式应用于每个文档所产生的最小值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$minN

Returns an aggregation of the n minimum valued elements in a group. 返回组中n个最小值元素的聚合。Distinct from the $minN array operator.$minN数组运算符不同。

New in version 5.2.在版本5.2中新增

Available in $group, $setWindowFields and as an expression.$group阶段和$setWindowFields阶段中可用,并用作表达式

$push

Returns an array of values that result from applying an expression to each document.返回将表达式应用于每个文档所产生的值数组。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$rank

Returns the document position (known as the rank) relative to other documents in the $setWindowFields stage partition.返回相对于$setWindowFields阶段分区中其他文档的文档位置(称为排名)。

New in version 5.0.在版本5.0中新增

$shift

Returns the value from an expression applied to a document in a specified position relative to the current document in the $setWindowFields stage partition.返回表达式中的值,该表达式应用于$setWindowFields阶段分区中相对于当前文档的指定位置的文档。

New in version 5.0.在版本5.0中新增

$stdDevPop

Returns the population standard deviation that results from applying a numeric expression to each document.返回将数值表达式应用于每个文档所产生的总体标准偏差。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$stdDevSamp

Returns the sample standard deviation that results from applying a numeric expression to each document.返回将数值表达式应用于每个文档所产生的标准偏差示例。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$sum

Returns the sum that results from applying a numeric expression to each document.返回将数值表达式应用于每个文档的结果之和。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$top

Returns the top element within a group according to the specified sort order.根据指定的排序顺序返回组中的顶部元素。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$topN

Returns an aggregation of the top n fields within a group, according to the specified sort order.根据指定的排序顺序,返回组中前n个字段的聚合。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

Alphabetical Listing of Expression Operators表达式运算符的字母顺序列表

Name名称Description描述
$absReturns the absolute value of a number.返回数字的绝对值。
$accumulatorReturns the result of a user-defined accumulator function.返回用户定义的累加器函数的结果。
$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 return a new date. 添加数字以返回总和,或添加数字和日期以返回新日期。If adding numbers and a date, treats the numbers as milliseconds. 如果添加数字和日期,则将这些数字视为毫秒。Accepts any number of argument expressions, but at most, one expression can resolve to a date.接受任意数量的参数表达式,但最多一个表达式可以解析为一个日期。
$addToSet

Returns an array of unique expression values for each group. 返回每个组的唯一表达式值数组。Order of the array elements is undefined.数组元素的顺序未定义。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$allElementsTrueReturns true if no element of a set evaluates to false, otherwise, returns false. 如果集合中没有元素的计算结果为false,则返回true,否则返回falseAccepts a single argument expression.接受单个参数表达式。
$andReturns true only when all its expressions evaluate to true. 仅当其所有表达式的计算结果均为true时,才返回trueAccepts any number of argument expressions.接受任意数量的参数表达式。
$anyElementTrueReturns true if any elements of a set evaluate to true; otherwise, returns false. 如果集合中的任何元素的计算结果为true,则返回true;否则,返回falseAccepts 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 the expression respectively.返回y/x的反切线(反正切),单位为弧度,其中yx分别是传递给表达式的第一个和第二个值。
$atanhReturns 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.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$binarySize

Returns the size of a given string or binary data value's content in bytes.返回给定字符串或二进制数据值内容的大小(字节)。

New in version 4.4.在版本4.4中新增

$bottom

Returns the bottom element within a group according to the specified sort order.根据指定的排序顺序返回组中的底部元素。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$bottomN

Returns an aggregation of the bottom n fields within a group, according to the specified sort order.

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$bsonSize

Returns the size in bytes of a given document (i.e. bsontype Object) when encoded as BSON.当编码为BSON时,返回给定文档(即bsontype Object)的字节大小。

New in version 4.4.在版本4.4中新增

$ceilReturns the smallest integer greater than or equal to the specified number.返回大于或等于指定数字的最小整数。
$cmpReturns: 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
$concatConcatenates any number of strings.连接任意数量的字符串。
$concatArraysConcatenates arrays to return the concatenated array.连接数组以返回连接的数组。
$condA 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.接受有序列表中的三个表达式或三个命名参数。
$convertConverts a value to a specified type.将值转换为指定的类型。
$cosReturns the cosine of a value that is measured in radians.返回以弧度为单位的值的余弦。
$coshReturns 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.在版本5.0中新增 Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

Distinct from the $count pipeline stage.有别于$count管道阶段。

$covariancePop

Returns the population covariance of two numeric expressions.返回两个数值表达式的总体协方差。

New in version 5.0.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$covarianceSamp

Returns the sample covariance of two numeric expressions.返回两个数值表达式的样本协方差。

New in version 5.0.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$dateAddAdds a number of time units to a date object.向日期对象添加多个时间单位。
$dateDiffReturns the difference between two dates.返回两个日期之间的差值。
$dateFromPartsConstructs a BSON Date object given the date's constituent parts.根据日期的组成部分构造BSON Date对象。
$dateSubtractSubtracts a number of time units from a date object.从日期对象中减去若干时间单位。
$dateTruncTruncates a date.截断日期。
$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.以1到31之间的数字返回日期的月份。
$dayOfWeekReturns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).以1(星期日)到7(星期六)之间的数字形式返回日期的星期几。
$dayOfYearReturns the day of the year for a date as a number between 1 and 366 (leap year).以介于1和366(闰年)之间的数字返回日期的一年中的某一天。
$degreesToRadiansConverts 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.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$derivative

Returns the average rate of change within the specified window.返回指定窗口内的平均更改率。

New in version 5.0.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$divideReturns 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.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$eqReturns true if the values are equivalent.如果值相等,则返回true
$expRaises e to the specified exponent.e幂乘到指定的指数。
$expMovingAvg

Returns the exponential moving average for the numeric expression.返回数值表达式的指数移动平均值。

New in version 5.0.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$filterSelects 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.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

Distinct from the $first array operator.有别于$first数组运算符。

$first

Returns the first array element.返回第一个数组元素。

New in version 4.4.在版本4.4中新增

Distinct from the $first accumulator.有别于$first累加器

$firstN

Returns a specified number of elements from the beginning of an array. 从数组的开头返回指定数量的元素。Distinct from the $firstN accumulator.有别于$firstN累加器

New in version 5.2.在版本5.2中新增

$firstN

Returns an aggregation of the first n fields within a group. 返回组中前n个字段的聚合。Only meaningful when documents are in a defined order. 仅当文档按定义的顺序排列时才有意义。Distinct from the $firstN array operator.有别于$firstN数组运算符。

New in version 5.2.在版本5.2中新增

Available in $group, expression and $setWindowFields stages.$group阶段、表达式$setWindowFields阶段中可用。

$floorReturns the largest integer less than or equal to the specified number.返回小于或等于指定数字的最大整数。
$function

Defines a custom aggregation function.定义自定义聚合函数。

New in version 4.4.在版本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 ($).可以使用$getField检索名称中包含句点(.)或者以美元符号($)开始的字段的值。

$gtReturns true if the first value is greater than the second.如果第一个值大于第二个值,则返回true
$gteReturns true if the first value is greater than or equal to the second.如果第一个值大于或等于第二个值,则返回true
$hourReturns the hour for a date as a number between 0 and 23.以0到23之间的数字返回日期的小时。
$ifNullReturns 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.第二个表达式的结果可以为null
$inReturns a boolean indicating whether a specified value is in an array.返回一个布尔值,指示指定的值是否在数组中。
$indexOfArraySearches 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.如果未找到子字符串,则返回-1
$indexOfBytesSearches a string for an occurrence of a substring and returns the UTF-8 byte index of the first occurrence. 在字符串中搜索子字符串的匹配项,并返回第一个匹配项的UTF-8字节索引。If the substring is not found, returns -1.如果未找到子字符串,则返回-1
$indexOfCPSearches a string for an occurrence of a substring and returns the UTF-8 code point index of the first occurrence. 在字符串中搜索子字符串的匹配项,并返回第一个匹配项的UTF-8代码点索引。If the substring is not found, returns -1.如果未找到子字符串,则返回-1
$integral

Returns the approximation of the area under a curve.返回曲线下面积的近似值。

New in version 5.0.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$isArrayDetermines if the operand is an array. 确定操作数是否为数组。Returns a boolean.返回一个布尔值。
$isNumberDetermines if the expression resolves to an integer, double, decimal, or long.确定表达式解析为整数、双精度、十进制还是长整型。
$isoDayOfWeekReturns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).以ISO 8601格式返回工作日编号,范围从1(周一)到7(周日)。
$isoWeekReturns the week number in ISO 8601 format, ranging from 1 to 53. 返回ISO 8601格式的周数,范围从153Week numbers start at 1 with the week (Monday through Sunday) that contains the year's first Thursday.周数从1开始,以包含一年中第一个星期四的一周(周一到周日)为单位。
$isoWeekYearReturns the year number in ISO 8601 format. 返回ISO 8601格式的年份号。The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601).一年从第一周的星期一开始(ISO 8601),到最后一周的星期日结束(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.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

Distinct from the $last array operator.有别于$last数组运算符。

$last

Returns the last array element.返回最后一个数组元素。

New in version 4.4.在版本4.4中新增

Distinct from the $last accumulator.有别于$last累加器。

$lastN

Returns a specified number of elements from the end of an array. 从数组末尾返回指定数量的元素。Distinct from the $lastN accumulator.有别于$lastN累加器。

New in version 5.2.在版本5.2中新增

$lastN

Returns an aggregation of the last n fields within a group. 返回组中最后n个字段的聚合。Only meaningful when documents are in a defined order. 仅当文档按定义的顺序排列时才有意义。Distinct from the $lastN array operator.$lastN数组运算符不同。

New in version 5.2.在版本5.2中新增

Available in $group, expression and $setWindowFields stages.$group阶段、表达式$setWindowFields阶段中可用。

$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.使用基于周围字段值的线性插值填充窗口中的null字段和缺失字段。

New in version 5.3.在版本5.3中新增

$literalReturn 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.例如,对以$开头的字符串使用$literal表达式,以避免解析为字段路径。
$lnCalculates 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.窗口null字段和缺失字段的值设置为该字段的最后一个非空值。

New in version 5.2.在版本5.2中新增

$logCalculates the log of a number in the specified base.计算指定基数中某个数字的对数。
$log10Calculates the log base 10 of a number.计算一个数字的对数基数10。
$ltReturns true if the first value is less than the second.如果第一个值小于第二个值,则返回true
$lteReturns true if the first value is less than or equal to the second.如果第一个值小于或等于第二个值,则返回true
$ltrimRemoves whitespace or the specified characters from the beginning of a string.删除字符串开头的空白或指定字符。
$mapApplies 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.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$maxN

Returns the n largest values in an array. 返回数组中的n个最大值。Distinct from the $maxN accumulator.有别于$maxN累加器。

New in version 5.2.在版本5.2中新增

$maxN

Returns an aggregation of the n maximum valued elements in a group. 返回组中n个最大值元素的聚合。Distinct from the $maxN array operator.有别于$maxN数组运算符。

New in version 5.2.在版本5.2中新增Available in $group, $setWindowFields and as an expression.$group阶段、$setWindowFields阶段中可用,并作为表达式使用

$mergeObjectsCombines multiple documents into a single document.将多个文档合并为一个文档。
$metaAccess available per-document metadata related to the aggregation operation.可访问与聚合操作相关的每个文档元数据。
$min

Returns the lowest expression value for each group.返回每个组的最低表达式值。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$minN

Returns the n smallest values in an array. 返回数组中的n个最小值。Distinct from the $minN accumulator.有别于$minN累加器。

New in version 5.2.在版本5.2中新增

$minN

Returns an aggregation of the n minimum valued elements in a group. 返回组中n个最小值元素的聚合。Distinct from the $minN array operator.有别于$minN数组运算符。

New in version 5.2.在版本5.2中新增 Available in $group, $setWindowFields and as an expression.$group阶段和$setWindowFields阶段中可用,并用作表达式

$millisecondReturns the milliseconds of a date as a number between 0 and 999.以0到999之间的数字形式返回日期的毫秒数。
$minuteReturns the minute for a date as a number between 0 and 59.以0到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).将日期的月份返回为介于1(1月)和12(12月)之间的数字。
$multiplyMultiplies numbers to return the product. Accepts any number of argument expressions.将数字相乘以返回产品。接受任意数量的参数表达式。
$neReturns true if the values are notequivalent.如果值相等,则返回true
$notReturns the boolean value that is the opposite of its argument expression. 返回与其参数表达式相反的布尔值。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. 当其任何表达式的计算结果为true时,返回trueAccepts any number of argument expressions.接受任意数量的参数表达式。
$powRaises a number to the specified exponent.将数字提升到指定的指数。
$push

Returns an array of expression values for documents in each group.返回每个组中文档的表达式值数组。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$radiansToDegreesConverts a value from radians to degrees.将值从弧度转换为度。
$randReturns a random float between 0 and 1.返回介于0和1之间的随机浮点值。
$rangeOutputs 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.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$reduceApplies an expression to each element in an array and combines them into a single value.将表达式应用于数组中的每个元素,并将它们组合为单个值。
$regexFindApplies a regular expression (regex) to a string and returns information on the first matched substring.将正则表达式(regex)应用于字符串,并返回有关第一个匹配子字符串的信息。
$regexFindAllApplies a regular expression (regex) to a string and returns information on the all matched substrings.将正则表达式(regex)应用于字符串,并返回有关所有匹配子字符串的信息。
$regexMatchApplies a regular expression (regex) to a string and returns a boolean that indicates if a match is found or not.将正则表达式(regex)应用于字符串,并返回一个布尔值,指示是否找到匹配项。
$replaceOne

Replaces the first instance of a matched string in a given input.替换给定输入中匹配字符串的第一个实例。

New in version 4.4.在版本4.4中新增

$replaceAll

Replaces all instances of a matched string in a given input.替换给定输入中匹配字符串的所有实例。

New in version 4.4.在版本4.4中新增

$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.从字符串末尾删除空格或指定字符。
$sampleRateRandomly 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.虽然每次运行时选择的文档的确切数量各不相同,但选择的数量近似于样本率,以文档总数的百分比表示。
$secondReturns the seconds for a date as a number between 0 and 60 (leap seconds).以0到60之间的数字(闰秒)返回日期的秒数。
$setDifferenceReturns 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.只接受两个参数表达式。
$setEqualsReturns true if the input sets have the same distinct elements. 如果输入集具有相同的不同元素,则返回trueAccepts 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 ($).可以使用$setField添加、更新或删除名称中包含句点(.)或者从美元符号($)开始。

New in version 5.0.在版本5.0中新增

$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 the second set, including when the first set equals the second set; i.e. not a strict subset. 如果第一个集合的所有元素都出现在第二个集合中,包括第一个集合等于第二个集合时;也就是说,不是严格的子集,则返回trueAccepts exactly two argument expressions.只接受两个参数表达式。
$setUnionReturns 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.在版本5.0中新增Available in $setWindowFields stage.$setWindowFields阶段中可用。

$sizeReturns the number of elements in the array. 返回数组中的元素数。Accepts a single expression as argument.接受单个表达式作为参数。
$sinReturns the sine of a value that is measured in radians.返回以弧度为单位的值的正弦值。
$sinhReturns the hyperbolic sine of a value that is measured in radians.返回以弧度度量的值的双曲正弦。
$sliceReturns a subset of an array.返回数组的子集。
$sortArraySorts an array based on its elements.根据数组的元素对数组进行排序。
$splitSplits 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.如果在字符串中找不到分隔符,则返回包含原始字符串的数组。
$sqrtCalculates the square root.计算平方根。
$stdDevPop

Returns the population standard deviation of the input values.返回输入值的总体标准偏差。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$stdDevSamp

Returns the sample standard deviation of the input values.返回输入值的样本标准偏差。

Changed in version 5.0.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$strcasecmpPerforms 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.执行不区分大小写的字符串比较,如果两个字符串相等,则返回0;如果第一个字符串大于第二个字符串,则返回1;如果第一个字符串小于第二个字符串,则返回-1
$strLenBytesReturns the number of UTF-8 encoded bytes in a string.返回字符串中UTF-8编码的字节数。
$strLenCPReturns the number of UTF-8 code points in a string.返回字符串中UTF-8代码点的数目。
$substrDeprecated. 不赞成。Use $substrBytes or $substrCP.请改用$substrBytes$substrCP
$substrBytesReturns 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.从字符串中指定的UTF-8字节索引(从零开始)处的字符开始,并持续指定的字节数。
$substrCPReturns 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.从字符串中指定的UTF-8代码点(CP)索引(从零开始)处的字符开始,并按指定的代码点数继续。
$subtractReturns 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.在版本5.0中更改

Available in $setWindowFields stage.$setWindowFields阶段中可用。

$switchEvaluates 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.当它找到一个计算结果为true的表达式时,$switch执行一个指定的表达式并中断控制流。
$tanReturns the tangent of a value that is measured in radians.返回以弧度为单位测量的值的切线。
$tanhReturns the hyperbolic tangent of a value that is measured in radians.返回以弧度度量的值的双曲正切。
$toBoolConverts value to a boolean.将值转换为布尔值。
$toDateConverts value to a Date.将值转换为日期。
$toDecimalConverts value to a Decimal128.将值转换为小数128。
$toDoubleConverts value to a double.将值转换为双精度。
$toIntConverts value to an integer.将值转换为整数。
$toLongConverts value to a long.将值转换为长字符串。
$toObjectIdConverts value to an ObjectId.将值转换为ObjectId。
$top

Returns the top element within a group according to the specified sort order.根据指定的排序顺序返回组中的顶部元素。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$topN

Returns an aggregation of the top n fields within a group, according to the specified sort order.根据指定的排序顺序,返回组中前n个字段的聚合。

New in version 5.2.在版本5.2中新增

Available in $group and $setWindowFields stages.$group阶段和$setWindowFields阶段中可用。

$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 beginning and end of a string.删除字符串开头和结尾的空白或指定字符。
$truncTruncates a number to a whole integer or to a specified decimal place.将数字截断为整数指定的小数位。
$tsIncrement

Returns the incrementing ordinal from a timestamp as a long.时间戳中的递增序号以long的形式返回。

New in version 5.1.在版本5.1中新增

$tsSecond

Returns the seconds from a timestamp as a long.时间戳中的秒数以long的形式返回。

New in version 5.1.在版本5.1中新增

$typeReturn the BSON data type of the field.返回字段的BSON数据类型。
$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 ($).$setField的别名,用于删除名称中包含句点(.)或者以美元符号($)开头的字段。

New in version 5.0.在版本5.0中新增

$weekReturns 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).将日期的周数返回为0(一年中第一个星期日之前的部分周)和53(闰年)之间的数字。
$yearReturns the year for a date as a number (e.g. 2014).以数字形式返回日期的年份(例如2014年)。
$zipMerge two arrays together.将两个数组合并在一起。

For the pipeline stages, see Aggregation Pipeline Stages.有关管道阶段,请参阅聚合管道阶段

←  $unwind (aggregation)$abs (aggregation) →