$trunc
New from version 8.0.1.
The $trunc operator in Amazon DocumentDB truncates a number to a specified decimal place, removing digits without rounding.
Parameters
number: An expression that resolves to a number.place: Optional. An integer expression between -20 and 100 that specifies the number of decimal places to truncate to. A negative value truncates to the left of the decimal. Defaults to 0.
Example (MongoDB Shell)
The following example demonstrates how to use the $trunc operator to truncate numeric values to one decimal place.
Truncating with a positive place value
db.measurements.insertMany([ {_id: 1, value: 3.456}, {_id: 2, value: 7.891}, {_id: 3, value: 12.345} ]);
Query example
db.measurements.aggregate([ { $project: { truncated: { $trunc: ["$value", 1] } } } ]);
Output
[
{_id: 1, truncated: 3.4},
{_id: 2, truncated: 7.8},
{_id: 3, truncated: 12.3}
]
Truncating with a negative place value
When place is negative, $trunc replaces digits to the left of the decimal with zeros. The following example truncates values using the first digit to the left of the decimal.
db.samples.insertMany([ {_id: 1, value: 19.25}, {_id: 2, value: 28.73}, {_id: 3, value: 34.32} ]);
db.samples.aggregate([ { $project: { truncated: { $trunc: ["$value", -1] } } } ]);
Output
[
{_id: 1, truncated: 10},
{_id: 2, truncated: 20},
{_id: 3, truncated: 30}
]
Code examples
To view a code example for using the $trunc operator, choose the tab for the language that you want to use: