Note
For details on specific operator, including syntax and examples, click on the specific operator to go to its reference page.有关特定运算符的详细信息,包括语法和示例,请单击特定运算符以转到其参考页。
In this section本节内容
$group
)$group
)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:通常,这些表达式采用参数数组,并具有以下形式:
If operator accepts a single argument, you can omit the outer array designating the argument list:如果运算符接受单个参数,则可以忽略指定参数列表的外部数组:
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 expressions perform mathematic operations on numbers. 算术表达式对数字执行数学运算。Some arithmetic expressions can also support date arithmetic.一些算术表达式也可以支持日期算术。
$abs |
|
$add |
|
$ceil |
|
$divide |
|
$exp |
|
$floor |
|
$ln |
|
$log |
|
$log10 |
|
$mod |
|
$multiply |
|
$pow |
|
$round |
|
$sqrt |
|
$subtract |
|
$trunc |
$arrayElemAt | |
$arrayToObject |
|
$concatArrays |
|
$filter |
|
$first |
$first accumulator.$first 累加器。 |
$in |
|
$indexOfArray |
-1 .-1 。 |
$isArray |
|
$last |
$last accumulator.$last 累加器。 |
$map |
|
$objectToArray |
|
$range |
|
$reduce |
|
$reverseArray |
|
$size |
|
$slice |
|
$zip |
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
布尔值外,布尔表达式的以下值的计算结果为假:null
、0
和undefined
值。The Boolean expression evaluates all other values as 布尔表达式将所有其他值计算为true
, including non-zero numeric values and arrays.true
,包括非零数值和数组。
$and |
true only when all its expressions evaluate to true . true 时,才返回true 。 |
$not |
|
$or |
true when any of its expressions evaluates to true . true 时,返回true 。 |
Comparison expressions return a boolean except for 比较表达式返回布尔值,$cmp
which returns a number.$cmp
除外,它返回一个数字。
The comparison expressions take two argument expressions and compare both value and type, using the specified BSON comparison order for values of different types.比较表达式采用两个参数表达式,并对不同类型的值使用指定的BSON比较顺序来比较值和类型。
$cmp | 0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second.0 ;如果第一个值大于第二个值,则返回1 ;如果第一个值小于第二个值,则返回-1 。 |
$eq |
true if the values are equivalent.true 。 |
$gt |
true if the first value is greater than the second.true 。 |
$gte |
true if the first value is greater than or equal to the second.true 。 |
$lt |
true if the first value is less than the second.true 。 |
$lte |
true if the first value is less than or equal to the second.true 。 |
$ne |
true if the values are not equivalent.true 。 |
$cond |
|
$ifNull |
Null 结果包含未定义值或缺少字段的实例。null 。 |
$switch |
true , $switch executes a specified expression and breaks out of the control flow.true 的表达式时,$switch 执行指定的表达式并中断控制流。 |
$accumulator |
|
$function |
|
The following operators return the size of a data element:以下运算符返回数据元素的大小:
$binarySize |
|
$bsonSize |
Object ) when encoded as BSON.Object )的大小(以字节为单位)。 |
The following operators returns date objects or components of a date object:以下运算符返回日期对象或日期对象的组件:
$dateFromParts |
|
$dateFromString |
|
$dateToParts |
|
$dateToString |
|
$dayOfMonth |
|
$dayOfWeek |
|
$dayOfYear |
|
$hour |
|
$isoDayOfWeek |
1 (for Monday) to 7 (for Sunday).1 (星期一)到7 (星期日)。 |
$isoWeek |
1 to 53 . 1 到53 。1 with the week (Monday through Sunday) that contains the year’s first Thursday.1 开始,以包含一年中第一个星期四的周(周一到周日)为单位。 |
$isoWeekYear |
|
$millisecond |
|
$minute |
|
$month |
|
$second |
|
$toDate |
|
$week |
|
$year |
The following arithmetic operators can take date operands:以下算术运算符可以采用日期操作数:
$add |
|
$subtract |
$literal |
$literal expression to a string that starts with a $ to avoid parsing as a field path.$ 开头的字符串使用$literal 表达式以避免解析为字段路径。 |
$mergeObjects |
|
$objectToArray |
|
Set expressions performs set operation on arrays, treating arrays as sets. 集合表达式对数组执行集合操作,将数组视为集合。Set expressions ignores the duplicate entries in each input array and the order of the elements.集合表达式忽略每个输入数组中的重复项以及元素的顺序。
If the set operation returns a set, the operation filters out duplicates in the result to output an array that contains only unique entries. 如果set操作返回一个set,则该操作会过滤掉结果中的重复项,以输出仅包含唯一项的数组。The order of the elements in the output array is unspecified.未指定输出数组中元素的顺序。
If a set contains a nested array element, the set expression does not descend into the nested array but evaluates the array at top-level.如果集合包含嵌套数组元素,则集合表达式不会下降到嵌套数组中,而是在顶级对数组求值。
$allElementsTrue |
true if no element of a set evaluates to false , otherwise, returns false . false ,则返回true ,否则返回false 。 |
$anyElementTrue |
true if any elements of a set evaluate to true ; otherwise, returns false . true ,则返回true ;否则,返回false 。 |
$setDifference |
|
$setEquals |
true if the input sets have the same distinct elements. true 。 |
$setIntersection |
|
$setIsSubset |
true if all elements of the first set appear in the second set, including when the first set equals the second set; i.e. not a strict subset. true ;亦即,不是严格的子集。 |
$setUnion |
String expressions, with the exception of 除了$concat
, only have a well-defined behavior for strings of ASCII characters.$concat
之外,字符串表达式对于ASCII字符的字符串只有定义良好的行为。
无论使用何种字符,$concat
behavior is well-defined regardless of the characters used.$concat
行为都是定义良好的。
$concat |
|
$dateFromString |
|
$dateToString |
|
$indexOfBytes |
-1 .-1 。 |
$indexOfCP |
-1 -1 |
$ltrim |
|
$regexFind |
|
$regexFindAll |
|
$regexMatch |
|
$replaceOne |
|
$replaceAll |
|
$rtrim |
|
$split |
|
$strLenBytes |
|
$strLenCP |
|
$strcasecmp |
0 if two strings are equivalent, 1 if the first string is greater than the second, and -1 if the first string is less than the second.0 ;如果第一个字符串大于第二个字符串,则返回1 ;如果第一个字符串小于第二个字符串,则返回-1 。 |
$substr |
$substrBytes or $substrCP . |
$substrBytes |
|
$substrCP |
|
$toLower |
|
$toString |
|
$trim |
|
$toUpper |
$meta |
Trigonometry expressions perform trigonometric operations on numbers. 三角表达式对数字执行三角运算。Values that represent angles are always input or output in radians. 表示角度的值始终以弧度输入或输出。Use 使用$degreesToRadians
and $radiansToDegrees
to convert between degree and radian measurements.$degreesToRadians
和$radiansToDegrees
在度和弧度测量之间进行转换。
$sin |
|
$cos |
|
$tan |
|
$asin |
|
$acos |
|
$atan |
|
$atan2 |
y / x in radians, where y and x are the first and second values passed to the expression respectively.y/x 的反切线(反正切),单位为弧度,其中y 和x 分别是传递给表达式的第一个值和第二个值。 |
$asinh |
|
$acosh |
|
$atanh |
|
$degreesToRadians |
|
$radiansToDegrees |
$convert |
|
$isNumber |
|
$toBool |
|
$toDate |
|
$toDecimal |
|
$toDouble |
|
$toInt |
|
$toLong |
|
$toObjectId |
|
$toString |
|
$type |
$group
)$group
)¶Available for use in the 可在$group
stage, accumulators are operators that maintain their state (e.g. totals, maximums, minimums, and related data) as documents progress through the pipeline.$group
阶段使用,累加器是在文档通过管道时保持其状态(例如总计、最大值、最小值和相关数据)的运算符。
When used as accumulators in the 在$group
stage, these operators take as input a single expression, evaluating the expression once for each input document, and maintain their stage for the group of documents that share the same group key.$group
阶段中用作累加器时,这些运算符将单个表达式作为输入,对每个输入文档计算一次表达式,并为共享同一组密钥的文档组维护其阶段。
$accumulator |
|
$addToSet |
|
$avg |
|
$first |
|
$last |
|
$max |
|
$mergeObjects |
|
$min |
|
$push |
|
$stdDevPop |
|
$stdDevSamp |
|
$sum |
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 3.2.在版本3.2中更改。
The following accumulator operators are also available in the 以下累加器运算符也可在$project
, $addFields
, and $set
stages.$project
、$addFields
和$set
阶段中使用。
$avg |
|
$max |
|
$min |
|
$stdDevPop |
|
$stdDevSamp |
|
$sum |
$abs |
|
$accumulator |
|
$acos |
|
$acosh |
|
$add |
|
$addToSet |
|
$allElementsTrue |
true if no element of a set evaluates to false , otherwise, returns false . false ,则返回true ,否则返回false 。 |
$and |
true only when all its expressions evaluate to true . Accepts any number of argument expressions.true 时,才返回true 。接受任意数量的参数表达式。 |
$anyElementTrue |
true if any elements of a set evaluate to true ; otherwise, returns false . true ,则返回true ;否则,返回false 。 |
$arrayElemAt |
|
$arrayToObject |
|
$asin |
|
$asinh |
|
$atan |
|
$atan2 |
y / x in radians, where y and x are the first and second values passed to the expression respectively.y/x 的反切线(反正切),单位为弧度,其中y 和x 分别是传递给表达式的第一个值和第二个值。 |
$atanh |
|
$avg |
|
$binarySize |
|
$bsonSize |
|
$ceil |
|
$cmp |
0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second.0 ;如果第一个值大于第二个值,则返回1;如果第一个值小于第二个值,则返回-1 。 |
$concat |
|
$concatArrays |
|
$cond |
|
$convert |
|
$cos |
|
$dateFromParts |
|
$dateToParts |
|
$dateFromString |
|
$dateToString |
|
$dayOfMonth |
|
$dayOfWeek |
|
$dayOfYear |
|
$degreesToRadians |
|
$divide |
|
$eq |
true if the values are equivalent.true 。 |
$exp |
|
$filter |
|
$first |
|
$first |
|
$floor |
|
$function |
|
$gt |
true if the first value is greater than the second.true 。 |
$gte |
true if the first value is greater than or equal to the second.true 。 |
$hour |
|
$ifNull |
Null 结果包含未定义值或缺少字段的实例。null 。 |
$in |
|
$indexOfArray |
-1 .-1 。 |
$indexOfBytes |
-1 .-1 。 |
$indexOfCP |
-1 .-1 。 |
$isArray |
|
$isNumber |
|
$isoDayOfWeek |
1 (for Monday) to 7 (for Sunday).1 (星期一)到7 (星期日)。 |
$isoWeek |
1 to 53 . 1 到53 。1 with the week (Monday through Sunday) that contains the year’s first Thursday.1 开始,以包含一年中第一个星期四的周(周一到周日)为单位。 |
$isoWeekYear |
|
$last |
|
$last |
|
$let |
|
$literal |
$literal expression to a string that starts with a $ to avoid parsing as a field path.$ 开头的字符串使用$literal 表达式以避免解析为字段路径。 |
$ln |
|
$log |
|
$log10 |
|
$lt |
true if the first value is less than the second.true 。 |
$lte |
true if the first value is less than or equal to the second.true 。 |
$ltrim |
|
$map |
|
$max |
|
$mergeObjects |
|
$meta |
|
$min |
|
$millisecond |
|
$minute |
|
$mod |
|
$month |
|
$multiply |
|
$ne |
true if the values are not equivalent.true 。 |
$not |
|
$objectToArray |
|
$or |
true when any of its expressions evaluates to true . Accepts any number of argument expressions.true 时,返回true 。接受任意数量的参数表达式。 |
$pow |
|
$push |
|
$radiansToDegrees |
|
$range |
|
$reduce |
|
$regexFind |
|
$regexFindAll |
|
$regexMatch |
|
$replaceOne |
|
$replaceAll |
|
$reverseArray |
|
$round |
|
$rtrim |
|
$second |
|
$setDifference |
|
$setEquals |
true if the input sets have the same distinct elements. true 。 |
$setIntersection |
|
$setIsSubset |
true if all elements of the first set appear in the second set, including when the first set equals the second set; i.e. not a strict subset. true 。 |
$setUnion |
|
$size |
|
$sin |
|
$slice |
|
$split |
|
$sqrt |
|
$stdDevPop |
|
$stdDevSamp |
|
$strcasecmp |
0 if two strings are equivalent, 1 if the first string is greater than the second, and -1 if the first string is less than the second.0 ;如果第一个字符串大于第二个字符串,则返回1 ;如果第一个字符串小于第二个字符串,则返回-1 。 |
$strLenBytes |
|
$strLenCP |
|
$substr |
$substrBytes or $substrCP .$substrBytes 或$substrCP 。 |
$substrBytes |
|
$substrCP |
|
$subtract |
|
$sum |
|
$switch |
true , $switch executes a specified expression and breaks out of the control flow.true 的表达式时,$switch 执行指定的表达式并中断控制流。 |
$tan |
|
$toBool |
|
$toDate |
|
$toDecimal |
|
$toDouble |
|
$toInt |
|
$toLong |
|
$toObjectId |
|
$toString |
|
$toLower |
|
$toUpper |
|
$trim |
|
$trunc |
|
$type |
|
$week |
|
$year |
|
$zip |
For the pipeline stages, see Aggregation Pipeline Stages.有关管道阶段,请参阅聚合管道阶段。