- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Arithmetic Aggregation Operators >
- $trunc (aggregation)
$trunc (aggregation)¶
On this page
Definition¶
-
$trunc¶ New in version 3.2.
Truncates a number to its integer.
$trunchas the following syntax:The
<number>expression can be any valid expression as long as it resolves to a number. For more information on expressions, see Expressions.
Behavior¶
If the argument resolves to a value of null or refers to a field that is
missing, $trunc returns null. If the argument resolves to
NaN, $trunc returns NaN.
| Example | Results |
|---|---|
{ $trunc: 0 } |
0 |
{ $trunc: 7.80 } |
7 |
{ $trunc: -2.3 } |
-2 |
Example¶
A collection named samples contains the following documents:
The following example returns both the original value and the truncated value:
The operation returns the following results: