$trunc (aggregation)

On this page本页内容

Definition定义

$trunc

Changed in version 4.2..

$trunc truncates a number to a whole integer or to a specified decimal place.将数字截断为整数或指定的小数位。

MongoDB 4.2 adds the following syntax for $trunc:MongoDB 4.2为$trunc添加了以下语法:

{ $trunc : [ <number>, <place> ] }
Field字段Type类型Description描述
<number> number

Can be any valid expression that resolves to a number. 可以是解析为数字的任何有效表达式Specifically, the expression must resolve to an integer, double, decimal, or long.具体来说,表达式必须解析为整数、双精度、十进制长整型

$trunc returns an error if the expression resolves to a non-numeric data type.如果表达式解析为非数字数据类型,则$trunc返回错误。

<place> integer

Optional Can be any valid expression that resolves to an integer between -20 and 100, exclusive. e.g. -20 < place < 100. 可选表达式可以是解析为-20到100之间(排除100,亦即-20 < place < 100)的整数的任何有效表达式Defaults to 0 if unspecified.如果未指定,则默认为0

  • If <place> resolves to a positive integer, $trunc truncates to <place> decimal places.如果<place>解析为正整数,$trunc将截断为<place>小数位。

    For example, $trunc : [1234.5678, 2] truncates to two decimal places and returns 1234.56.例如,$trunc : [1234.5678, 2]截断到小数点后两位,并返回1234.56

  • If <place> resolves to a negative integer, $trunc replaces <place> digits left of the decimal with 0.如果<place>解析为负整数,$trunc将小数点左边的<place>位数替换为0。

    For example, $trunc : [1234.5678, -2] replaces to two digits left of the decimal with 0 and returns 1200.例如,$trunc : [1234.5678, -2]将小数点左边的两位数替换为0,并返回1200

    If the absolute value of <place> exceeds the number of digits to the left of the decimal, $trunc returns 0.如果<place>的绝对值超过小数点左边的位数,$trunc返回0。

    For example, $trunc : [ 1234.5678, -5] specifies the fifth digit left of the decimal. 例如,$trunc : [ 1234.5678, -5]指定小数点左边的第五位数字。This exceeds the number of digits left of the decimal and returns 0.这超过了小数点后的位数,返回0

  • If <place> resolves to 0, trunc truncates all digits to the right of the decimal and returns the whole integer value.如果<place>解析为0trunc将截断小数点右侧的所有数字,并返回整个整数值。

    For example, $trunc : [1234.5678, 0] returns 1234例如,$trunc:[1234.5678, 0]返回1234

Prior to MongoDB 4.2, $trunc truncated the input value to the whole integer. 在MongoDB 4.2之前,$trunc将输入值截断为整数。MongoDB 4.2 continues supporting the pre-4.2 syntax and behavior:MongoDB 4.2继续支持4.2之前的语法和行为:

{ $trunc: <number> }

The <number> expression can be any valid expression as long as it resolves to a number. <number>表达式可以是任何有效的表达式,只要它解析为一个数字。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

$trunc does not round the truncated data. 不舍入截断的数据。To round input values to a specified place, use the $round expression.要将输入值舍入到指定的位置,请使用$round表达式。

Returned Data Type返回的数据类型

If truncating to a specific decimal place, the data type returned by $trunc matches the data type of the input expression or value.如果截断到特定的小数位,$trunc返回的数据类型与输入表达式或值的数据类型匹配。

If truncating to a whole integer value, $trunc returns an integer.如果截断为整数值,$trunc返回一个整数。

null, NaN, and +/- InfinitynullNaN+/- Infinity

  • If the argument resolves to a value of null or refers to a field that is missing, $trunc returns null.如果参数解析为null值或引用缺少的字段,$trunc返回null
  • If the argument resolves to NaN, $trunc returns NaN.如果参数解析为NaN,则$trunc返回NaN
  • If the argument resolves to negative or positive infinity, $trunc returns negative or positive infinity respectively.如果参数解析为负无穷大或正无穷大,$trunc分别返回负无穷大或正无穷大。
Example示例Results结果
{ $trunc: [ NaN, 1] } NaN
{ $trunc: [ null, 1] } null
{ $trunc : [ Infinity, 1 ] } Infinity
{ $trunc : [ -Infinity, 1 ] } -Infinity

Example示例

A collection named samples contains the following documents:名为samples的集合包含以下文档:

{ _id: 1, value: 19.25 }
{ _id: 2, value: 28.73 }
{ _id: 3, value: 34.32 }
{ _id: 4, value: -45.34 }