MongoDB $trunc Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $trunc?

$trunc is a MongoDB aggregation expression that truncates a number toward zero to a chosen decimal place—without rounding. Use it when you must drop extra digits (prices, sensor readings, tax) instead of rounding them up.

Remember
$trunc = Math.trunc for aggregation (+ place)

{ $trunc: [ <number>, <place> ] }

21.578, place 2 → 21.57   // no round-up
-3.789, place 0 → -3      // toward zero
place omitted → 0         // integer truncate

vs $round → may round up
vs $floor → negatives go down
UsePrefer
Drop digits (no round-up)$trunc
Standard rounding$round
Always down (incl. negatives)$floor

How to Use $trunc in MongoDB

Pass a number and an optional place, then keep or project the truncated result.

  1. Choose an expression stage ($addFields or $project).
  2. Pass the numeric field (or nested math expression) as the first argument.
  3. Set place to how many digits to keep after the decimal (0 = integer).
  4. Remember: result always moves toward zero, never rounds away from it.
PieceRequired?Role
NumberYesField, literal, or numeric expression
PlaceNoDigits after decimal; default 0
$truncYesTruncate toward zero at that place

Pseudocode

Pseudocode
for each document:
    n = evaluate(number)
    p = evaluate(place) or 0
    if n is null or missing:
        result = null
    else:
        factor = 10 ^ p
        result = trunc(n * factor) / factor   // toward zero
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while truncating numbers in a pipeline.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $trunc: [ <number>, <place> ] }
DirectionAlways toward zero
place = 221.578 → 21.57
place = 0-3.789 → -3
place = -21234 → 1200 (hundreds)
Nullnull / missing → null

Live Preview

Watch $trunc drop digits toward zero. Pick a sample value and place.

Simulates $trunc (and shows $round for comparison).

Result
n = 21.578, place = 2

$trunc → 21.57
$round → 21.58

{ truncated: 21.57 }

Worked Walkthrough — Sensor Reading

One reading stores value: 21.578. Goal: keep two decimal places without rounding up.

Calculation
Document:
  { sensor: "Temp-A", value: 21.578 }

place = 2
factor = 10^2 = 100

21.578 * 100 = 2157.8
trunc toward zero → 2157
2157 / 100 → 21.57

$round would give 21.58
$trunc gives 21.57 ✓

MongoDB Examples

We use a small readings collection. First truncate to two decimals, then truncate to an integer toward zero. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine three temperature sensors. Values include decimals and one negative reading so we can see toward-zero behavior.

mongosh
db.readings.insertMany([
  { _id: 1, sensor: "Temp-A", value: 21.578 },
  { _id: 2, sensor: "Temp-B", value: 18.942 },
  { _id: 3, sensor: "Temp-C", value: -3.789 }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps three documents in readings. Run find() to see them:

mongosh
db.readings.find()

Read each document like this:

Field in MongoDBExamplePlain English
_id1Document id (row number for this lesson)
sensor"Temp-A"Sensor name
value21.578Raw reading (extra decimal digits)

How It Works

  1. Insert once. insertMany adds three reading documents to readings.
  2. Values stay precise. MongoDB stores the full decimals until you truncate them.
  3. find() shows the same shape. What you inserted is what you get back—ready for $trunc next.

Example 2 — Truncate Decimals

Goal: keep two digits after the decimal without rounding up. 21.578 should become 21.57, not 21.58.

mongosh
db.readings.aggregate([
  {
    $addFields: {
      truncated: { $trunc: ["$value", 2] }
    }
  }
]);

How It Works

  1. Read the stored value. Temp-A has value: 21.578 from Example 1.
  2. Keep two places. $trunc: ["$value", 2] drops digits after the second decimal.
  3. No round-up. The next digit is 8, but $trunc still returns 21.57 (unlike $round).

Example 3 — Truncate to Integer

Same readings, but use place: 0 to drop the fractional part toward zero—especially clear on the negative value.

mongosh
db.readings.aggregate([
  {
    $addFields: {
      asInt: { $trunc: ["$value", 0] },
      floored: { $floor: "$value" }
    }
  }
]);

How It Works

  1. Same stored documents. Still using the three readings from Example 1.
  2. Toward zero. $trunc turns -3.789 into -3.
  3. Compare with floor. $floor turns the same value into -4 (toward −∞).
  4. Easy check. Positive values match floor here; the negative row shows the difference.

Edge Cases & Pitfalls

Check these before using $trunc in production pipelines.

round

Not the same as $round

If the next digit is 5–9, $round may increase the kept digit. $trunc never does.

neg

Negatives go toward zero

-3.7 → -3 with $trunc, but -4 with $floor. Pick the operator that matches your rule.

null

Null and missing become null

Missing fields return null, not zero. Guard with $ifNull if you need a default.

place

Negative place is allowed

place: -2 truncates to hundreds (1234 → 1200). Useful for coarse buckets without rounding.

Performance Notes

ApproachCostBest for
$trunc alonePer-document mathPrecision limits / display
Nested expression + $truncStill O(docs)Averages, tax, computed fields
Truncate lateFull precision earlierPipelines that need exact math first

Truncation is cheap compared with large sorts or joins. Filter early with $match when only some documents need a truncated field.

Key Takeaways

  • Role: truncate toward zero—no rounding up.
  • Place: digits after the decimal (0 = integer).
  • vs $round: use $trunc when chop-only is required.
  • vs $floor: negatives differ (-3.7 → -3 vs -4).

One line: $trunc answers “what is this number with extra digits chopped off toward zero?”

Frequently Asked Questions

$trunc removes digits beyond a chosen decimal place without rounding. It always moves toward zero—so 21.578 at place 2 becomes 21.57, not 21.58.
Use { $trunc: [ <number>, <place> ] } or { $trunc: { input: <expression>, place: <expression> } }. place is optional and defaults to 0 (integer truncation toward zero).
$trunc chops extra digits toward zero. $round uses rounding rules. At place 2, 21.578 → 21.57 with $trunc, but 21.58 with $round.
$trunc moves toward zero. -3.7 at place 0 becomes -3. $floor would return -4 because it goes toward negative infinity.
$trunc returns null when the number expression is null or missing. It does not throw an error for null input.
Yes for place 0. Think of $trunc as MongoDB’s aggregation version of Math.trunc, with an optional place for decimal precision—run it in the pipeline, not as a standalone find() filter.

Next: $type

Return the BSON type name of a field in an aggregation pipeline.

$type tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful