- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $trunc (aggregation)
$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
: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
.$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 < place < 100
. Defaults to 0 if unspecified.If
<place>
resolves to a positive integer,$trunc
truncates to<place>
decimal places.For example,
$trunc : [1234.5678, 2]
truncates to two decimal places and returns1234.56
.If
<place>
resolves to a negative integer,$trunc
replaces<place>
digits left of the decimal with0
.For example,
$trunc : [1234.5678, -2]
replaces to two digits left of the decimal with0
and returns1200
.If the absolute value of
<place>
exceeds the number of digits to the left of the decimal,$trunc
returns0
.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 returns0
.If
<place>
resolves to0
,trunc
truncates all digits to the right of the decimal and returns the whole integer value.For example,
$trunc : [1234.5678, 0]
returns1234
Prior to MongoDB 4.2,
$trunc
truncated the input value to the whole integer. MongoDB 4.2 continues supporting the pre-4.2 syntax and behavior:The
<number>
expression can be any valid expression as long as it resolves to a 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.
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.
If truncating to a whole integer value, $trunc
returns
an integer.
null
, NaN
, and +/- Infinity
¶
- If the argument resolves to a value of
null
or refers to a field that is missing,$trunc
returnsnull
. - If the argument resolves to
NaN
,$trunc
returnsNaN
. - If the argument resolves to negative or positive infinity,
$trunc
returns negative or positive infinity respectively.
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:
The following aggregation returns
value
truncated to the first decimal place:The operation returns the following results:
The following aggregation returns
value
truncated to the first place:The operation returns the following results:
The following aggregation returns``value`` truncated to the whole integer:
The operation returns the following results: