$trunc (aggregation)
On this page本页内容
Definition定义
$trunc
Changed in version 4.2..4.2.版更改。$trunc
truncates a number to a whole integer or to a specified decimal place.将数字截断为整整数或指定的小数位数。MongoDB 4.2 adds the following syntax forMongoDB 4.2为$trunc
:$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
, orlong
.integer
、double
、decimal
或long
。
$trunc
returns an error if the expression resolves to a non-numeric data type.如果表达式解析为非数字数据类型,则返回错误。<place>
integer Optional.可选的。Can be any valid expression that resolves to an integer between -20 and 100, exclusive. e.g.可以是解析为介于-20和100之间的整数的任何有效表达式。例如-20 < place < 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 returns1234.56
.$trunc : [1234.5678, 2]
截断到小数点后两位并返回1234.56
。If如果<place>
resolves to a negative integer,$trunc
replaces<place>
digits left of the decimal with0
.<place>
解析为负整数,$trunc
将小数点左侧的<place>
数字替换为0。
For example,例如,$trunc : [1234.5678, -2]
replaces to two digits left of the decimal with0
and returns1200
.$trunc : [1234.5678, -2]
将小数点后两位替换为0
,并返回1200
。
If the absolute value of如果<place>
exceeds the number of digits to the left of the decimal,$trunc
returns0
.<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 to0
,$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]
returns1234
$trunc : [1234.5678, 0]
返回1234
。
Prior to MongoDB 4.2,在MongoDB 4.2之前,$trunc
truncated the input value to the whole integer. MongoDB 4.2 continues supporting the pre-4.2 syntax and behavior:$trunc
将输入值截断为整数。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 +/- Infinity
If the argument resolves to a value of如果参数解析为null
or refers to a field that is missing,$trunc
returnsnull
.null
值或引用了缺失的字段,$trunc
将返回null
。If the argument resolves to如果参数解析为NaN,NaN
,$trunc
returnsNaN
.$trunc
将返回NaN
。If the argument resolves to negative or positive infinity,如果参数解析为负无穷大或正无穷大,$trunc
returns negative or positive infinity respectively.$trunc
将分别返回负无穷大和正无穷大。
{ $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 }