Sample Input Documents
Suppose you have a readings collection:
[
{ "_id": 1, "sensor": "Temp-A", "value": 21.578 },
{ "_id": 2, "sensor": "Temp-B", "value": 18.942 },
{ "_id": 3, "sensor": "Temp-C", "value": -3.789 }
] 
The $trunc operator truncates numeric values toward zero to a chosen number of decimal places inside MongoDB aggregation pipelines. Use it when you need to chop extra digits without rounding up — for example, limiting display precision or applying strict billing rules.
Chop, don’t round.
Decimals or tens.
Positive & negative.
Add truncated fields.
Trunc vs round.
Know null behavior.
In MongoDB’s aggregation framework, the $trunc operator removes digits beyond a specified decimal place without rounding. It always truncates toward zero. For example, truncating 21.578 to two places gives 21.57 (where $round would give 21.58).
This is useful when business rules require dropping fractional digits rather than rounding, such as conservative billing, fixed-precision exports, or display formatting where rounding would change totals unexpectedly.
Think of $trunc like cutting off digits with scissors — it never rounds up. For standard rounding, use $round. For always rounding down, use $floor.
The $trunc operator accepts an array of two expressions:
{ $trunc: [ <number>, <place> ] } Alternatively, you can use the object form:
{
$trunc: {
input: <number expression>,
place: <place expression>
}
} place) — how many decimal places to keep.place: 2 — keep two decimal places by truncating the rest (e.g. 21.578 → 21.57).place: 0 — truncate to a whole number toward zero.place — truncate to tens, hundreds, etc. (-1 → nearest 10 toward zero).$project, $addFields, or $set.null, the result is null.$trunc → 21.57, $round → 21.58$trunc → -3, $floor → -4$trunc → 4, $round → 5$round| Question | Answer |
|---|---|
| Operator type | Aggregation expression operator (arithmetic) |
| Syntax | { $trunc: [ number, place ] } |
| Behavior | Truncates toward zero (no rounding up) |
| Output | Truncated number (or null) |
| Common stages | $project, $addFields, $set |
{
$trunc: ["$price", 2]
}Chop after 2nd decimal
{
$trunc: ["$score", 0]
}Whole number toward zero
{
$trunc: ["$sales", -2]
}place: -2
nullReturns null
Truncate sensor readings, compare with $round, handle negative values, and limit computed averages with $trunc.
Truncate readings to two decimal places without rounding up.
Suppose you have a readings collection:
[
{ "_id": 1, "sensor": "Temp-A", "value": 21.578 },
{ "_id": 2, "sensor": "Temp-B", "value": 18.942 },
{ "_id": 3, "sensor": "Temp-C", "value": -3.789 }
] Limit precision without rounding:
db.readings.aggregate([
{
$project: {
sensor: 1,
value: 1,
truncatedValue: {
$trunc: ["$value", 2]
}
}
}
]) place: 2 keeps two digits after the decimal and drops the rest. Unlike $round, 21.578 becomes 21.57, not 21.58.
Understand how $trunc differs from other numeric operators.
See the difference on the same values:
db.products.aggregate([
{
$project: {
price: 1,
truncated: { $trunc: ["$price", 2] },
rounded: { $round: ["$price", 2] }
}
}
])
// price: 49.999 → truncated: 49.99, rounded: 50.00
// price: 29.451 → truncated: 29.45, rounded: 29.45 When the next digit would cause a round-up, $trunc and $round diverge. Choose based on whether your business rule allows rounding or requires chopping digits.
$trunc behaves differently from $floor on negatives:
db.readings.aggregate([
{
$project: {
value: 1,
truncated: { $trunc: ["$value", 0] },
floored: { $floor: "$value" }
}
}
])
// value: -3.7 → truncated: -3, floored: -4
// value: 4.9 → truncated: 4, floored: 4 $trunc always moves toward zero. This matters for signed decimals like temperature below freezing, altitude, or financial adjustments.
Truncate the result of a division expression:
db.orders.aggregate([
{
$group: {
_id: "$customerId",
total: { $sum: "$amount" },
count: { $sum: 1 }
}
},
{
$project: {
avgOrder: {
$trunc: [
{ $divide: ["$total", "$count"] },
2
]
}
}
}
])
// total: 100, count: 3 → avgOrder: 33.33 (not 33.34 from rounding) The first argument can be any numeric expression. Here, $divide computes the average and $trunc limits precision without rounding up.
Use a negative place for coarse buckets:
db.stores.aggregate([
{
$project: {
store: 1,
monthlySales: 1,
salesTruncated: {
$trunc: ["$monthlySales", -2]
}
}
}
])
// monthlySales: 1234.56 → salesTruncated: 1200
// monthlySales: 1999.99 → salesTruncated: 1900 place: -2 truncates to the hundreds place toward zero. Unlike $round, values just below a boundary stay in the lower bucket.
Truncate tax instead of rounding up when rules require it:
db.invoices.aggregate([
{
$project: {
item: 1,
subtotal: 1,
taxTruncated: {
$trunc: [
{ $multiply: ["$subtotal", 0.0825] },
2
]
},
taxRounded: {
$round: [
{ $multiply: ["$subtotal", 0.0825] },
2
]
}
}
}
])
// subtotal: 19.99 → tax at 8.25%:
// raw: 1.649175
// taxTruncated: 1.64
// taxRounded: 1.65 Multiply first, then truncate. This pattern shows why operator choice matters in financial pipelines.
$trunc WorksThe pipeline resolves the number and the place value to numbers (or null).
Extra decimal digits are removed toward zero. No rounding-up step occurs.
The result is stored in your projected field for display or further math.
Use when chopping digits is required instead of standard rounding.
The $trunc operator truncates numeric values toward zero to a specified decimal place in MongoDB aggregation pipelines. It is the right choice when you need to drop extra digits without rounding up.
Remember: positive place for decimals, zero for whole numbers, negative for tens and hundreds. Compare with $round and $floor before choosing. Next in the series: $type.
$trunc when business rules forbid rounding up$round on sample data before deploying$trunc rounds — it chops digits toward zero$trunc when standard rounding is required (use $round)$trunc with $floor on negative numbers$trunc as a query filter (it is expression-only)$truncUse these points when limiting numeric precision in pipelines.
Chop, don’t round.
PurposeTwo operands.
Syntax21.578 → 21.57.
Compare-3.7 → -3.
Edge caseNot just fields.
InputMove on to $type for BSON type inspection, or review $round for standard rounding.
5 people found this page helpful