On this page本页内容
$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 MongoDB 4.2为$trunc:$trunc添加了以下语法:
<number> |
number |
|
<place> |
integer |
|
Prior to MongoDB 4.2, 在MongoDB 4.2之前,$trunc truncated the input value to the whole integer. $trunc将输入值截断为整数。MongoDB 4.2 continues supporting the pre-4.2 syntax and behavior:MongoDB 4.2继续支持4.2之前的语法和行为:
The <number> expression can be any valid expression as long as it resolves to a number. <number>表达式可以是任何有效的表达式,只要它解析为一个数字。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
$trunc does not round the truncated data. 不舍入截断的数据。To round input values to a specified place, use the 要将输入值舍入到指定的位置,请使用$round expression.$round表达式。
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 +/- Infinitynull、NaN和+/- Infinity¶null or refers to a field that is missing, $trunc returns null.null值或引用缺少的字段,$trunc返回null。NaN, $trunc returns NaN.NaN,则$trunc返回NaN。$trunc returns negative or positive infinity respectively.$trunc分别返回负无穷大或正无穷大。{ $trunc: [ NaN, 1] } |
NaN |
{ $trunc: [ null, 1] } |
null |
{ $trunc : [ Infinity, 1 ] } |
Infinity |
{ $trunc : [ -Infinity, 1 ] } |
-Infinity |
A collection named 名为samples contains the following documents:samples的集合包含以下文档:
value truncated to the first decimal place:value:
The operation returns the following results:操作返回以下结果:
value truncated to the first place:The operation returns the following results:操作返回以下结果:
value truncated to the whole integer:value:
The operation returns the following results:操作返回以下结果: