Definition
$trunc$trunctruncates a number to a whole integer or to a specified decimal place.
Syntax
The $trunc operator has the following syntax:
{ $trunc : [ <number>, <place> ] }
Field | Type | Description |
|---|---|---|
| number | Can be any valid expression
that resolves to a number. Specifically, the expression must
resolve to an integer, double,
|
| integer | Optional Can be any valid expression
that resolves to an integer between -20 and 100, exclusive. For example,
|
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
The returned data type matches the data type of the input expression or value.
Truncation Precision with Floating Point Numbers
If you print a double-precision floating point number 4.56 with 20
digits after the decimal separator, you will see
4.55999999999999960920, which is slightly less than 4.56 and is
truncated to 4.55.
MongoDB uses the IEEE 754 standard for floating point computations, and the behavior is consistent with that standard.
If you need a floating point number for an application that requires
high precison, consider a Decimal128 value. For details, see
BSON.Decimal128.
If you need to store a currency value, consider an integer using the lowest currency denomination unit. For example, use an integer with cents or pennies instead of a floating point number.
null, NaN, and +/- Infinity
If the argument resolves to a value of
nullor refers to a field that is missing,$truncreturnsnull.If the argument resolves to
NaN,$truncreturnsNaN.If the argument resolves to negative or positive infinity,
$truncreturns negative or positive infinity respectively.
Example | Results |
|---|---|
|
|
|
|
|
|
|
|
Example
Create a collection named samples with the following documents:
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
valuetruncated to the first decimal place: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
valuetruncated to the first place: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:
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 }