Database Manual / Reference / Query Language

Expressions表达式

Expressions are MQL components that resolve to a value. Expressions are stateless, meaning they return a value without mutating any of the values used to build the expression. You can use expressions in the following MQL contexts:表达式是解析为值的MQL组件。表达式是无状态的,这意味着它们返回的值不会改变用于构建表达式的任何值。您可以在以下MQL上下文中使用表达式:

In the MongoDB Query Language, you can build expressions from the following components:在MongoDB查询语言中,您可以从以下组件构建表达式:

Component组件Example示例
Constants常量3
Operators运算符$add
Field path expressions字段路径表达式"$<path.to.field>"

For example, { $add: [ 3, "$inventory.total" ] } is an expression that consists of the $add operator and two operands:例如,{ $add: [ 3, "$inventory.total" ] }是一个由$add运算符和两个操作数组成的表达式:

The expression returns the result of adding 3 to the value at path inventory.total of the input document.该表达式返回将输入文档的路径inventory.total处的值加3的结果。

Expression operators are similar to functions that take arguments. In general, these operators take an array of arguments and have the following form:表达式运算符类似于接受参数的函数。一般来说,这些运算符接受一组参数,并具有以下形式:

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

If an operator accepts a single argument, you can omit the outer array designating the argument list:如果运算符接受单个参数,则可以省略指定参数列表的外部数组:

{ <operator>: <argument> }

This page lists operators that you can use to construct expressions.此页面列出了可用于构造表达式的运算符。

Arithmetic 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.将数字四舍五入为整数指定的小数位。
$sigmoidReturns the result of the sigmoid function (the integration of the normal distribution with standard deviation 1).返回sigmoid函数的结果(正态分布与标准偏差1的积分)。
$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 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. Distinct from the $maxN accumulator.返回数组中n个最大值。与$maxN累加器不同。
$minNReturns the n smallest values in an array. Distinct from the $minN accumulator.返回数组中n个最小值。与$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.返回对intlong值数组进行按位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 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. Accepts any number of argument expressions.仅当其所有表达式的计算结果均为true时,才返回true。接受任意数量的参数表达式。
$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. Accepts any number of argument expressions.当其任何表达式的计算结果为true时,返回true。接受任意数量的参数表达式。

Comparison 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 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结果,或者如果第一个表达式产生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 Operators自定义聚合运算符

Name名称Description描述
$accumulatorDefines a custom accumulator function.定义自定义累加器函数。
$functionDefines a custom function.定义自定义函数。

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 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. 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. The year starts with the Monday of week 1 (ISO 8601) and ends with the Sunday of the last week (ISO 8601).以ISO 8601格式返回年份号。一年从第1周的星期一开始(ISO 8601),到最后一周的星期日结束(ISO 86001)。
$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.将值转换为日期。
$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.接受两个参数表达式。如果这两个值是日期和数字,请先指定日期参数,因为从数字中减去日期没有意义。

Expressions Associated with Accumulators与累加器相关的表达式

Some accumulators for the $group stage are also available for use as expressions. When used as expressions, they calculate an aggregate value over the given input arguments or input array.$group阶段的一些累加器也可用作表达式。当用作表达式时,它们会计算给定输入参数或输入数组的聚合值。

The following operators are accumulators, but they are also available as expressions which accept an array of values as input.以下运算符是累加器,但它们也可用作接受值数组作为输入的表达式。

Name名称Description描述
$avgReturns an average of the specified expression or list of expressions for each document. Ignores non-numeric values.返回每个文档的指定表达式或表达式列表的平均值。忽略非数值。
$concatArraysReturns a single array that combines the elements of two or more arrays.返回一个组合了两个或多个数组元素的数组。
New in version 8.1.8.1版本中的新功能。
$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版本中的新功能。
$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版本中的新功能。
$setUnionTakes two or more arrays and returns an array containing the elements that appear in any input array.接受两个或多个数组,并返回一个包含出现在任何输入数组中的元素的数组。
New in version 8.1.8.1版本中的新功能。
$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.返回数值之和。忽略非数值。

Literal Expression Operators文字表达式运算符

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检索名称包含句点(.)或以美元符号($)开头的字段的值。
$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.以给定的速率随机选择文档。虽然每次运行时所选文档的确切数量各不相同,但所选数量近似于以文档总数百分比表示的采样率。
$toHashedIndexKeyComputes and returns the hash of the input expression using the same hash function that MongoDB uses to create a hashed index.使用MongoDB用于创建哈希索引的相同哈希函数计算并返回输入表达式的哈希值。

Object 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 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. The order of the elements in the output array is unspecified.如果set操作返回一个set,则该操作会筛选掉结果中的重复项,以输出一个仅包含唯一条目的数组。输出数组中元素的顺序未指定。

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. Accepts a single argument expression.如果集合中没有元素的计算结果为false,则返回true,否则返回false。接受单个参数表达式。
$anyElementTrueReturns true if any elements of a set evaluate to true; otherwise, returns false. Accepts a single argument expression.如果集合中的任何元素计算结果为true,则返回true;否则,返回false。接受单个参数表达式。
$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. Accepts two or more argument expressions.如果输入集具有相同的不同元素,则返回true。接受两个或多个参数表达式。
$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 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. If the substring is not found, returns -1.在字符串中搜索子字符串的出现,并返回第一个出现的UTF-8字节索引。如果未找到子字符串,则返回-1
$indexOfCPSearches a string for an occurrence of a substring and returns the UTF-8 code point index of the first occurrence. If the substring is not found, returns -1.在字符串中搜索子字符串的出现,并返回第一个出现的UTF-8码位索引。如果未找到子字符串,则返回-1
$ltrimRemoves whitespace or the specified characters from the beginning of a string.从字符串开头删除空格或指定字符。
$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.替换给定输入中匹配字符串的第一个实例。
$replaceAllReplaces all instances of a matched string in a given input.替换给定输入中匹配字符串的所有实例。
$rtrimRemoves whitespace or the specified characters from the end of a string.从字符串末尾删除空格或指定字符。
$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.将值转换为字符串。
$trimRemoves whitespace or the specified characters from the beginning and end of a string.从字符串的开头和结尾删除空格或指定字符。
$toUpperConverts a string to uppercase. Accepts a single argument expression.将字符串转换为大写。接受单个参数表达式。

Encrypted String Operators加密字符串运算符

Encrypted string expressions evaluate an argument against an encrypted field in a collection with Queryable Encryption enabled, and return a boolean.加密字符串表达式在启用了可查询加密的集合中对加密字段的参数进行求值,并返回一个布尔值。

Name名称Description描述
$encStrContainsReturns true if a subset of characters in the encrypted string match the specified string.如果加密字符串中的字符子集与指定字符串匹配,则返回true
$encStrEndsWithReturns true if the last characters of the encrypted string match the specified string.如果加密字符串的最后一个字符与指定字符串匹配,则返回true
$encStrNormalizedEqReturns true if the normalized string form of the encrypted string matches normalized string form of the specified string.如果加密字符串的 规范化字符串形式与指定字符串的规范性字符串形式匹配,则返回true
$encStrStartsWithReturns true if the first characters of the encrypted string match the specified string.如果加密字符串的前几个字符与指定字符串匹配,则返回true

Text Operators文本运算符

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

Timestamp 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 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.返回以弧度为单位的值的逆sin(弧正弦)。
$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 Operators类型运算符

Name名称Description描述
$convertConverts a value to a specified type.将值转换为指定类型。
$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
$toBoolConverts value to a boolean.将值转换为布尔值。
$toDateConverts value to a Date.将值转换为日期。
$toDecimalConverts value to a Decimal128.将值转换为Decimal128。
$toDoubleConverts value to a double.将值转换为双精度。
$toIntConverts value to an integer.将值转换为整数。
$toLongConverts value to a long.将值转换为long。
$toObjectIdConverts value to an ObjectId.将值转换为ObjectId。
$toStringConverts value to a string.将值转换为字符串。
$typeReturn the BSON data type of the field.返回字段的BSON数据类型。
$toUUIDConverts a string to a UUID.将字符串转换为UUID。

Variable 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窗口运算符

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.0: Available in the $setWindowFields stage.在5.0版本中更改:$setWindowFields阶段可用。
$avgReturns the average for the specified expression. Ignores non-numeric values.返回指定表达式的平均值。忽略非数值。
Changed in version 5.0: Available in the $setWindowFields stage.在5.0版本中更改:$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. Ties result in different adjacent document numbers.返回文档在$setWindowFields阶段分区中的位置(称为文档编号)。绑定会导致相邻的文档编号不同。
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.0: Available in the $setWindowFields stage.在5.0版本中更改:$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.0: Available in the $setWindowFields stage.在5.0版本中更改:$setWindowFields阶段可用。
$linearFillFills null and missing fields in a window using linear interpolation based on surrounding field values.使用基于周围字段值的线性插值填充窗口中的空字段和缺失字段。
New in version 5.3.5.3版本中的新功能:Available in $setWindowFields.$setWindowFields中可用。
$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版本中的新功能:Available in the $setWindowFields stage.$setWindowFields阶段可用。
$maxReturns the maximum value that results from applying an expression to each document.返回将表达式应用于每个文档所得到的最大值。
Changed in version 5.0: Available in the $setWindowFields stage.在5.0版本中更改:$setWindowFields阶段可用。
$minReturns the minimum value that results from applying an expression to each document.返回将表达式应用于每个文档所得到的最小值。
Changed in version 5.0: Available in the $setWindowFields stage.在5.0版本中更改:$setWindowFields阶段可用。
$minMaxScalerScales the value that results from applying an expression to each document.缩放将表达式应用于每个文档所得到的值。
New in version 8.2.8.2版本中的新功能:Available in the $setWindowFields stage.$setWindowFields阶段可用。
$minNReturns an aggregation of the n minimum valued elements in a group. Distinct from the $minN array operator.返回组中n个最小值元素的聚合。与$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.0: Available in the $setWindowFields stage.在5.0版本中更改:$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.0: Available in the $setWindowFields stage.在5.0版本中更改:$setWindowFields阶段可用。
$stdDevSampReturns the sample standard deviation that results from applying a numeric expression to each document.返回将数值表达式应用于每个文档时产生的样本标准偏差。
Changed in version 5.0: Available in the $setWindowFields stage.在5.0版本中更改:$setWindowFields阶段可用。
$sumReturns the sum that results from applying a numeric expression to each document.返回对每个文档应用数值表达式得到的总和。
Changed in version 5.0: Available in the $setWindowFields stage.在5.0版本中更改:$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阶段可用。