Database Manual / Reference / Query Language / Expressions

$trunc (aggregation)(聚合)

Definition定义

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

Syntax语法

The $trunc operator has the following syntax:$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.具体来说,表达式必须解析为intdoubledecimallong

$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. 可选。可以是解析为-20100之间的整数的任何有效表达式,不包括-20100在内。For example, -20 < place < 100. Defaults to 0 if unspecified.例如,-20 < place < 100。如果未指定,则默认为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. This exceeds the number of digits left of the decimal and returns 0.例如,$trunc : [ 1234.5678, -5]指定小数点后的第五位数字。这超过了小数点后的位数,返回0

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

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

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.$trunc不会对截断的数据进行四舍五入。要将输入值四舍五入到指定位置,请使用$round表达式。

Returned Data Type返回的数据类型

The returned data type matches the data type of the input expression or value.返回的数据类型与输入表达式或值的数据类型匹配。

null, NaN, and +/- 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示例

Create a collection named samples with the following documents:使用以下文档创建一个名为samples的集合:

db.samples.insertMany(
[
{ _id: 1, value: 19.25 },
{ _id: 2, value: 28.73 },
{ _id: 3, value: 34.32 },
{ _id: 4, value: -45.34 }
]
)
  • The following aggregation returns value truncated to the first decimal place:以下聚合返回截断到第一位小数的value

    db.samples.aggregate([
    { $project: { truncatedValue: { $trunc: [ "$value", 1 ] } } }
    ])

    The operation returns the following results:该操作返回以下结果:

    { "_id" : 1, "truncatedValue" : 19.2 }
    { "_id" : 2, "truncatedValue" : 28.7 }
    { "_id" : 3, "truncatedValue" : 34.3 }
    { "_id" : 4, "truncatedValue" : -45.3 }
  • The following aggregation returns value truncated to the first place:以下聚合返回截断到第一位的value

    db.samples.aggregate([
    { $project: { truncatedValue: { $trunc: [ "$value", -1 ] } } }
    ])

    The operation returns the following results:该操作返回以下结果:

    { "_id" : 1, "truncatedValue" : 10 }
    { "_id" : 2, "truncatedValue" : 20 }
    { "_id" : 3, "truncatedValue" : 30 }
    { "_id" : 4, "truncatedValue" : -40 }
  • The following aggregation returns value truncated to the whole integer:以下聚合返回截断为整数的value

    db.samples.aggregate([
    { $project: { truncatedValue: { $trunc: [ "$value", 0 ] } } }
    ])

    The operation returns the following results:该操作返回以下结果:

    { "_id" : 1, "truncatedValue" : 19 }
    { "_id" : 2, "truncatedValue" : 28 }
    { "_id" : 3, "truncatedValue" : 34 }
    { "_id" : 4, "truncatedValue" : -45 }