Docs HomeMongoDB Manual

Aggregation Pipeline Operators聚合管道运算符

Note

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

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.选择数组的子集以返回一个仅包含与筛选条件匹配的元素的数组。
$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. 在数组中搜索指定值的引用,并返回第一个引用的数组索引。Array indexes start at zero.数组索引从零开始。
$isArrayDetermines if the operand is an array. 确定操作数是否为数组。Returns a boolean.返回布尔值。
$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.将两个数组合并在一起。

Bitwise Operators按位运算符

Name名称Description描述
$bitAndReturns the result of a bitwise and operation on an array of int or long values. 返回对int值或long值数组执行按位and运算的结果。
New in version 6.3. 6.3版新增。
$bitNotReturns the result of a bitwise not operation on a single argument or an array that contains a single int or long value. 返回对单个参数或包含单个intlong值的数组执行逐位not运算的结果。
New in version 6.3. 6.3版新增。
$bitOrReturns the result of a bitwise or operation on an array of int or long values. 返回对intlong值数组执行按位or运算的结果。
New in version 6.3. 6.3版新增。
$bitXorReturns the result of a bitwise xor (exclusive or) operation on an array of int and long values. 返回对intlong值数组执行逐位xor(异或)运算的结果。
New in version 6.3. 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布尔值之外,布尔表达式还将以下值计算为falsenull0undefined义值。The 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结果,如果第一个表达式导致null结果,则返回第二个表达式的结果。Null result encompasses instances of undefined values or missing fields. Null结果包含未定义值或缺少字段的实例。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描述
$accumulatorDefines a custom accumulator function. 定义自定义累加器函数。
New in version 4.4. 4.4版新增。
$functionDefines 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对象)的大小(以字节为单位)。

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 860 1)。
$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(一月)和12(十二月)之间的数字形式返回日期的月份。
$secondReturns the seconds for a date as a number between 0 and 60 (leap seconds).以介于0和60(闰秒)之间的数字形式返回日期的秒数。
$toDateConverts value to a Date. 将值转换为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描述
$getFieldReturns 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.将文档转换为表示键值对的文档数组。
$setFieldAdds, 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.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.如果集合包含嵌套数组元素,则集合表达式不会下降到嵌套数组中,而是在顶级计算数组。

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. Accepts exactly two argument expressions.如果第一个集合的所有元素都出现在第二个集合中,包括当第一个集合等于第二个集时,返回true;即不是严格的子集。正好接受两个参数表达式。
$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.无论使用什么字符,行为都是定义良好的。

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
$ltrimRemoves whitespace or the specified characters from the beginning of a string. 删除字符串开头的空白或指定字符。
New in version 4.0. 4.0版新增。
$regexFindApplies a regular expression (regex) to a string and returns information on the first matched substring.将正则表达式(regex)应用于字符串,并返回第一个匹配子字符串的信息。
New in version 4.2. 4.2版新增。
$regexFindAllApplies a regular expression (regex) to a string and returns information on the all matched substrings. 将正则表达式(regex)应用于字符串,并返回所有匹配子字符串的信息。
New in version 4.2. 4.2版新增。
$regexMatchApplies 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版新增。
$replaceOneReplaces the first instance of a matched string in a given input. 替换给定输入中匹配字符串的第一个实例。
New in version 4.4. 4.4版新增。
$replaceAllReplaces all instances of a matched string in a given input. 替换给定输入中匹配字符串的所有实例。
New in version 4.4. 4.4版新增。
$rtrimRemoves 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.将字符串转换为小写。接受单个参数表达式。
$toStringConverts value to a string. 将值转换为字符串。
New in version 4.0. 4.0版新增。
$trimRemoves 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描述
$tsIncrementReturns the incrementing ordinal from a timestamp as a long. long形式返回时间戳中的递增序号。
New in version 5.1. 5.1版新增。
$tsSecondReturns 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.返回以弧度为单位的值的反正弦(arc-sine)。
$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描述
$convertConverts a value to a specified type. 将值转换为指定的类型。
New in version 4.0. 4.0版新增。
$isNumberReturns 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版新增。
$toBoolConverts value to a boolean. 将值转换为boolean
New in version 4.0. 4.0版新增。
$toDateConverts value to a Date. 将值转换为Date
New in version 4.0. 4.0版新增。
$toDecimalConverts value to a Decimal128. 将值转换为Decimal128
New in version 4.0. 4.0版新增。
$toDoubleConverts value to a double. 将值转换为double
New in version 4.0. 4.0版新增。
$toIntConverts value to an integer. 将值转换为integer
New in version 4.0. 4.0版新增。
$toLongConverts value to a long. 将值转换为long
New in version 4.0. 4.0版新增。
$toObjectIdConverts value to an ObjectId. 将值转换为ObjectId
New in version 4.0. 4.0版新增。
$toStringConverts 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:聚合累加器运算符:

  • 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:可用于以下聚合管道阶段:

Changed in version 5.05.0版更改.

Name名称Description描述
$accumulatorReturns the result of a user-defined accumulator function.返回用户定义的累加器函数的结果。
$addToSetReturns an array of unique expression values for each group. 为每个组返回一个唯一的表达式值数组。Order of the array elements is undefined. 数组元素的顺序未定义。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$avgReturns an average of numerical values. 返回数值的平均值。Ignores non-numeric values. 忽略非数值。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$bottomReturns the bottom element within a group according to the specified sort order. 根据指定的排序顺序返回组中的底部元素。
New in version 5.2. 5.2版新增。
Available in the $group and $setWindowFields stages. $group$setWindowFields阶段中可用。
$bottomNReturns 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 the $group and $setWindowFields stages. $group$setWindowFields阶段中可用。
$countReturns the number of documents in a group.返回一个组中的文档数。
Distinct from the $count pipeline stage. $count管道阶段不同。
New in version 5.05.0版新增: Available in the $group and $setWindowFields stages. :在$group$setWindowFields阶段中可用。
$firstReturns the result of an expression for the first document in a group. 返回组中第一个文档的表达式结果。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$firstNReturns 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.25.2版新增: Available in the $group, expression and $setWindowFields stages. :在$group表达式$setWindowFields阶段中可用。
$lastReturns the result of an expression for the last document in a group. 返回组中最后一个文档的表达式结果。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$lastNReturns 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.25.2版新增: Available in the $group, expression and $setWindowFields stages. :在$group表达式$setWindowFields阶段中可用。
$maxReturns the highest expression value for each group. 返回每组的最高表达式值。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$maxNReturns 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中可用,并用作表达式
$medianReturns an approximation of the median, the 50th percentile, as a scalar value. 以标量值的形式返回中值(第50个百分位数)的近似值。
New in version 7.0. 7.0版新增。
This operator is available as an accumulator in these stages:此操作器在以下阶段可用作累加器: It is also available as an aggregation expression. 它也可用作聚合表达式
$mergeObjectsReturns a document created by combining the input documents for each group.返回通过组合每组的输入文档创建的文档。
$minReturns the lowest expression value for each group. 返回每组的最低表达式值。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$percentileReturns an array of scalar values that correspond to specified percentile values. 返回与指定百分位数相对应的标量值数组。
New in version 7.0. 7.0版新增。
This operator is available as an accumulator in these stages:此操作器在以下阶段可用作累加器: It is also available as an aggregation expression. 它也可用作聚合表达式
$pushReturns an array of expression values for documents in each group. 为每组中的文档返回一个表达式值数组。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$stdDevPopReturns the population standard deviation of the input values. 返回输入值的总体标准偏差。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$stdDevSampReturns the sample standard deviation of the input values. 返回输入值的标准偏差样本。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$sumReturns a sum of numerical values. Ignores non-numeric values. 返回数值的总和。忽略非数值。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$topReturns the top element within a group according to the specified sort order. 根据指定的排序顺序返回组中的顶部元素。
New in version 5.2. 5.2版新增。
Available in the $group and $setWindowFields stages. $group$setWindowFields阶段中可用。
$topNReturns 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 the $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.05.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.返回每个文档的指定表达式或表达式列表的平均值。忽略非数值。
$firstReturns the result of an expression for the first document in a group.返回组中第一个文档的表达式结果。
$lastReturns the result of an expression for the last document in a group.返回组中最后一个文档的表达式结果。
$maxReturns the maximum of the specified expression or list of expressions for each document返回每个文档的指定表达式或表达式列表的最大值
$medianReturns an approximation of the median, the 50th percentile, as a scalar value. 以标量值的形式返回中值(第50个百分位数)的近似值。
New in version 7.0. 7.0版新增。
This operator is available as an accumulator in these stages:此操作器在以下阶段可用作累加器: It is also available as an aggregation expression. 它也可用作聚合表达式
$minReturns the minimum of the specified expression or list of expressions for each document返回每个文档的指定表达式或表达式列表的最小值
$percentileReturns an array of scalar values that correspond to specified percentile values. 返回与指定百分位数相对应的标量值数组。
New in version 7.0. 7.0版新增。
This operator is available as an accumulator in these stages:此操作器在以下阶段可用作累加器: It is also available as an aggregation expression. 它也可用作聚合表达式
$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描述
$letDefines 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描述
$addToSetReturns an array of all unique values that results from applying an expression to each document. 返回一个数组,该数组包含将表达式应用于每个文档所产生的所有唯一值。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$avgReturns the average for the specified expression. 返回指定表达式的平均值。Ignores non-numeric values. 忽略非数值。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$bottomReturns the bottom element within a group according to the specified sort order. 根据指定的排序顺序返回组中的底部元素。
New in version 5.2. 5.2版新增。
Available in the $group and $setWindowFields stages. $group$setWindowFields阶段中可用。
$bottomNReturns 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 the $group and $setWindowFields stages. $group$setWindowFields阶段中可用。
$countReturns the number of documents in the group or window.返回组或窗口中的文档数。
Distinct from the $count pipeline stage. $count管道阶段不同。
New in version 5.0. 5.0版新增。
$covariancePopReturns the population covariance of two numeric expressions.返回两个数值表达式的总体协方差。
New in version 5.0. 5.0版新增。
$covarianceSampReturns the sample covariance of two numeric expressions. 返回两个数值表达式的样本协方差。
New in version 5.0. 5.0版新增。
$denseRankReturns 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版新增。
$derivativeReturns the average rate of change within the specified window. 返回指定窗口内的平均变化率。
New in version 5.0. 5.0版新增。
$documentNumberReturns 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版新增。
$expMovingAvgReturns the exponential moving average for the numeric expression. 返回数值表达式的指数移动平均值。
New in version 5.0. 5.0版新增。
$firstReturns the result of an expression for the first document in a group or window. 返回组或窗口中第一个文档的表达式结果。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$integralReturns the approximation of the area under a curve. 返回曲线下面积的近似值。
New in version 5.0. 5.0版新增。
$lastReturns the result of an expression for the last document in a group or window. 返回组或窗口中最后一个文档的表达式结果。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$linearFillFills null and missing fields in a window using linear interpolation based on surrounding field values.使用基于周围字段值的线性插值填充窗口中的null字段和缺失字段。
Available in the $setWindowFields stage. $setWindowFields阶段中可用。
New in version 5.3. 5.3版新增。
$locfLast observation carried forward. 最后一次观察继续进行。Sets values for null and missing fields in a window to the last non-null value for the field.窗口null和缺失字段的值设置为该字段的最后一个非null值。
Available in the $setWindowFields stage. $setWindowFields阶段中可用。
New in version 5.2. 5.2版新增。
$maxReturns the maximum value that results from applying an expression to each document. 返回将表达式应用于每个文档所产生的最大值。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$minReturns the minimum value that results from applying an expression to each document. 返回将表达式应用于每个文档所产生的最小值。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$minNReturns 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中可用,用作表达式提供。
$pushReturns an array of values that result from applying an expression to each document. 返回将表达式应用于每个文档所产生的值的数组。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$rankReturns the document position (known as the rank) relative to other documents in the $setWindowFields stage partition. 返回文档相对于$setWindowFields阶段分区中其他文档的位置(称为排名)。
New in version 5.0. 5.0版新增。
$shiftReturns 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版新增。
$stdDevPopReturns the population standard deviation that results from applying a numeric expression to each document. 返回对每个文档应用数值表达式所产生的总体标准偏差。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$stdDevSampReturns the sample standard deviation that results from applying a numeric expression to each document. 返回对每个文档应用数值表达式所产生的标准偏差示例。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$sumReturns the sum that results from applying a numeric expression to each document. 返回将数值表达式应用于每个文档所产生的总和。
Changed in version 5.05.0版更改: Available in the $setWindowFields stage. :在$setWindowFields阶段中可用。
$topReturns the top element within a group according to the specified sort order. 根据指定的排序顺序返回组中的顶部元素。
New in version 5.2. 5.2版新增。
Available in the $group and $setWindowFields stages. $group$setWindowFields阶段中可用。
$topNReturns 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 the $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.接受任意数量的参数表达式,但最多只能有一个表达式解析为一个日期。
$addToSetReturns 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.返回以弧度为单位的值的反双曲正切(双曲正切)。
$avgReturns an average of numerical values. 返回数值的平均值。Ignores non-numeric values. 忽略非数值。
Changed in version 5.0. 5.0版更改。Available in $setWindowFields stage. $setWindowFields阶段可用。
$binarySizeReturns the size of a given string or binary data value's content in bytes. 返回给定字符串或二进制数据值内容的大小(以字节为单位)。
New in version 4.4. 4.4版新增。
$bitAndReturns the result of a bitwise and operation on an array of int or long values. 返回对int值或long值数组执行按位and运算的结果。
New in version 6.3. 6.3版新增。
$bitNotReturns the result of a bitwise not operation on a single argument or an array that contains a single int or long value. 返回对单个参数或包含单个intlong值的数组执行按位not运算的结果。
New in version 6.3. 6.3版新增。
$bitOrReturns the result of a bitwise or operation on an array of int or long values. 返回对intlong值数组执行按位或运算的结果。
New in version 6.3. 6.3版新增。
$bitXorReturns the result of a bitwise xor (exclusive or) operation on an array of int and long values. 返回对intlong值数组执行按位xor(异或)运算的结果。
New in version 6.3. 6.3版新增。
$bottomReturns 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阶段中可用。
$bottomNReturns 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阶段中可用。
$bsonSizeReturns the size in bytes of a given document (i.e. bsontype Object) when encoded as BSON. 当编码为BSON时,返回给定文档(即bsontype对象)的大小(以字节为单位)。
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.返回以弧度为单位测量的值的双曲余弦。
$countReturns 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管道阶段不同。
$covariancePopReturns the population covariance of two numeric expressions. 返回两个数值表达式的总体协方差。
New in version 5.0. 5.0版新增。Available in $setWindowFields stage. $setWindowFields阶段可用。
$covarianceSampReturns 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.将值从度数转换为弧度。
$denseRankReturns 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阶段可用。
$derivativeReturns 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.接受两个参数表达式。
$documentNumberReturns 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幂乘到指定的指数。
$expMovingAvgReturns 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.选择数组的子集以返回一个仅包含与筛选条件匹配的元素的数组。
$firstReturns 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阶段可用。
$firstNReturns a specified number of elements from the beginning of an array. 从数组的开头返回指定数量的元素。Distinct from the $firstN accumulator. $firstN累加器不同。
New in version 5.2. 5.2版新增。
$firstNReturns an aggregation of the first n fields within a group. 返回组中前n个字段的聚合。Results are 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.返回小于或等于指定数字的最大整数。
$functionDefines a custom aggregation function. 定义自定义聚合函数。
New in version 4.4. 4.4版新增。
$getFieldReturns 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结果,如果第一个表达式导致null结果,则返回第二个表达式的结果。Null result encompasses instances of undefined values or missing fields. Accepts two expressions as arguments. Null结果包含未定义值或缺少字段的实例。接受两个表达式作为参数。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. 在数组中搜索指定值的引用,并返回第一个引用的数组索引。Array indexes start at zero.数组索引从零开始。
$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
$integralReturns 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.确定表达式解析是否为integerdoubledecimallong之一。
$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. Week numbers start at 1 with the week (Monday through Sunday) that contains the year's first Thursday.返回ISO 8601格式的周数,范围从153。周数从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 860 1)。
$lastReturns 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阶段可用。
$lastNReturns a specified number of elements from the end of an array. 从数组末尾返回指定数量的元素。Distinct from the $lastN accumulator. $lastN累加器不同。
New in version 5.2. 5.2版新增。
$lastNReturns an aggregation of the last n fields within a group. 返回一个组中最后n个字段的聚合。Results are 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阶段中可用。
$letDefines 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. 接受任意数量的参数表达式。
$linearFillFills 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.计算一个数字的自然对数。
$locfLast observation carried forward. 最后一次观察继续进行。Sets values for null and missing fields in a window to the last non-null value for the field. 窗口null和缺失字段的值设置为该字段的最后一个非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.接受命名参数。
$maxReturns the highest expression value for each group. 返回每组的最高表达式值。
Changed in version 5.0. 5.0版更改。Available in $setWindowFields stage. $setWindowFields阶段可用。
$maxNReturns the n largest values in an array. 返回数组中最大的n个值。Distinct from the $maxN accumulator. $maxN累加器不同。
New in version 5.2. 5.2版新增。
$maxNReturns 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中可用,用作表达式
$medianReturns an approximation of the median, the 50th percentile, as a scalar value. 以标量值的形式返回中值(第50个百分位数)的近似值。
New in version 7.0. 7.0版新增。
This operator is available as an accumulator in these stages:此操作器在以下阶段可用作累加器: It is also available as an aggregation expression.它也可用作聚合表达式
$mergeObjectsCombines multiple documents into a single document.将多个文档合并为一个文档。
$metaAccess available per-document metadata related to the aggregation operation.与聚合操作相关的每个文档元数据的可用访问权限。
$minReturns the lowest expression value for each group. 返回每组的最低表达式值。
Changed in version 5.0. 5.0版更改。Available in $setWindowFields stage. $setWindowFields阶段可用。
$minNReturns the n smallest values in an array. 返回数组中的n个最小值。Distinct from the $minN accumulator. $minN累加器不同。
New in version 5.2. 5.2版新增。
$minNReturns 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(一月)和12(十二月)之间的数字形式返回日期的月份。
$multiplyMultiplies numbers to return the product. Accepts any number of argument expressions.将数字相乘以返回乘积。接受任意数量的参数表达式。
$neReturns true if the values are not equivalent.如果值相等,则返回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.接受任意数量的参数表达式。
$percentileReturns an array of scalar values that correspond to specified percentile values. 返回与指定百分位数相对应的标量值数组。
New in version 7.0. 7.0版新增。
This operator is available as an accumulator in these stages:此操作器在以下阶段可用作累加器: It is also available as an aggregation expression. 它也可用作聚合表达式
$powRaises a number to the specified exponent.将数字提升到指定的指数。
$pushReturns 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.根据用户定义的输入输出包含整数序列的数组。
$rankReturns 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)应用于字符串,并返回一个布尔值,指示是否找到匹配项。
$replaceOneReplaces the first instance of a matched string in a given input. 替换给定输入中匹配字符串的第一个实例。
New in version 4.4. 4.4版新增。
$replaceAllReplaces 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.接受两个或多个参数表达式。
$setFieldAdds, 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. 如果第一个集合的所有元素都出现在第二个集合中,包括当第一个集合等于第二个集时,返回true;即不是严格的子集Accepts exactly two argument expressions.正好接受两个参数表达式。
$setUnionReturns a set with elements that appear in any of the input sets.返回一个集,该集包含出现在任何输入集中的元素。
$shiftReturns 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.计算平方根。
$stdDevPopReturns the population standard deviation of the input values. 返回输入值的总体标准偏差。
Changed in version 5.0. 5.0版更改。Available in $setWindowFields stage. $setWindowFields阶段可用。
$stdDevSampReturns 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.如果这两个值是日期和数字,请先指定日期参数,因为从数字中减去日期没有意义。
$sumReturns 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.将值转换为boolean
$toDateConverts value to a Date.将值转换为Date
$toDecimalConverts value to a Decimal128.将值转换为Decimal128
$toDoubleConverts value to a double.将值转换为double
$toIntConverts value to an integer.将值转换为integer
$toLongConverts value to a long.将值转换为long
$toObjectIdConverts value to an ObjectId.将值转换为ObjectId
$topReturns 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阶段中可用。
$topNReturns 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.将数字截断为整整数或指定的小数位数。
$tsIncrementReturns the incrementing ordinal from a timestamp as a long. long形式返回时间戳中的递增序号。
New in version 5.1. 5.1版新增。
$tsSecondReturns 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数据类型。
$unsetFieldRemoves 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.有关管道阶段,请参阅聚合管道阶段