Optional: multiply by price or sort on the converted int.
If strings may be invalid, prefer $convert with onError.
Piece
Required?
Role
$toInt
Yes
Convert expression → int (or null)
Expression
Yes
Int string, number (truncated), bool, or int
$convert
No
Same conversion with error/null handling
Pseudocode
Pseudocode
for each document:
x = evaluate(expression)
if x is null or missing:
result = null
else if x is double/decimal:
result = truncateTowardZero(x) // must fit int32
else if x is integer string / bool / int:
result = int(x)
else:
error // e.g. "2.5" string
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while converting to integers.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $toInt: <expression> }
Shorthand for
{ $convert: { input: <e>, to: "int" } }
String example
"5" → 5; "2.5" → error
Truncation
1.99999 → 1 (toward zero)
Boolean
true → 1, false → 0
Range
32-bit int; use $toLong if larger
Try it
Live Preview
Pick an input and see the int-style result (browser demo of the same idea).
Same conversion. Stage one builds convertedQty like Example 2.
Multiply.$multiply does 5 × 10 = 50 and 10 × 20 = 200.
Project a slim result.$project keeps item and totalPrice.
Why convert first? Multiplying a string quantity would fail or behave unexpectedly—convert, then calculate.
Edge Cases & Pitfalls
Check these before using $toInt in production pipelines.
strings
"2.5" as a string fails
String inputs must be whole integers. Use $toDouble first (then $toInt), or $convert with onError.
truncate
Doubles truncate, they do not round
3.9 → 3 and -3.9 → -3. Use $round first if you need nearest-integer behavior.
range
Stay inside int32
Values outside the 32-bit range error. Prefer $toLong for larger whole numbers.
null
Null and missing become null
$toInt returns null for null/missing inputs—it does not become zero.
Analysis
Performance Notes
Approach
Cost
Best for
$toInt per query
Per-document convert
Cleaning imported qty strings
Store ints
Cheapest long-term
Fields you multiply/sum often
$convert + onError
Slightly more work
Dirty / nullable inputs
Prefer storing integer types when you control writes. Converting on every read is fine for cleanup, but avoid repeated string→int work on hot paths.
Remember
Key Takeaways
Role: convert a value to a 32-bit integer.
Doubles: truncate toward zero (not round).
Strings: whole integers only—"2.5" errors.
Bigger ints: use $toLong.
One line:$toInt answers “turn this into a 32-bit whole number I can count and multiply.”
Frequently Asked Questions
$toInt converts a value to a 32-bit integer inside an aggregation pipeline. Use it in stages such as $project and $addFields.
$toInt truncates toward zero. For example, 3.9 becomes 3 and -3.9 becomes -3. It does not round to the nearest integer.
$toInt produces a 32-bit integer (about ±2.1 billion). $toLong produces a 64-bit integer for larger whole numbers.
$toInt is shorthand for { $convert: { input: <expr>, to: "int" } }. Use $convert with onError when invalid values should return a fallback instead of failing the pipeline.
No. String inputs must be base-10 integers such as "5" or "-2". Decimal-looking strings like "2.5" cause a conversion error.
$toInt returns null when the input is null or missing. It does not throw an error for null input.