The $toInt operator converts values to a 32-bit integer in MongoDB aggregation pipelines. Use it to parse whole-number strings, truncate decimals, and enable integer math on imported or mixed-type data.
01
Integer Conversion
Values to int32.
02
Syntax
One expression.
03
Truncation
Doubles → whole nums.
04
$project Stage
Parse qty fields.
05
Use Cases
Counts, IDs, imports.
06
vs $toLong
32-bit vs 64-bit.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $toInt operator converts an expression to a 32-bit signed integer. Imported data often stores quantities, counts, and IDs as strings like "42" or "100". $toInt parses those into whole numbers for sorting, counting, and integer arithmetic.
$toInt is shorthand for { $convert: { input: <expression>, to: "int" } }. When converting doubles, it truncates toward zero — so 3.9 becomes 3, not 4.
💡
Beginner Tip
Int32 range is roughly -2.1 billion to 2.1 billion. For larger whole numbers (like big user IDs or timestamps), use $toLong instead.
$toInt errors on invalid strings. $convert with onError lets the pipeline continue with a safe default.
Applications
🚀 Use Cases
CSV/JSON import cleanup — parse string quantities and counts into integers.
Inventory management — sum stock levels and filter low-quantity items.
Counting and tallies — convert text counts for $sum and grouping.
Truncating decimals — drop fractional parts from double fields.
Schema migration — normalize legacy string-number fields in aggregation pipelines.
🧠 How $toInt Works
1
MongoDB reads the expression
The input resolves from a field path, numeric string, number, boolean, or nested expression.
Input
2
Value is converted to int32
Strings are parsed; doubles and decimals are truncated toward zero; booleans become 0 or 1.
Convert
3
An integer result is returned
The output supports $add, $sum, sorting, and integer comparisons.
Output
=
🔢
Whole number ready
Use in counts, sums, filters, and integer arithmetic pipelines.
Wrap Up
Conclusion
The $toInt operator converts values to 32-bit integers in MongoDB aggregation pipelines. It is ideal for parsing whole-number strings, truncating decimals, and enabling integer math on mixed-type data.
For values outside the int32 range, use $toLong. For decimal fractions, use $toDouble. For conversions with error handling, use $convert. Next in the series: $toLong.
Use $toInt for counts, quantities, and small whole numbers
Convert before $sum when fields are stored as strings
Use $round first when you need rounding, not truncation
Use $convert with onError for messy imported data
Switch to $toLong when values exceed int32 range
❌ Don’t
Expect $toInt to round (it truncates toward zero)
Use $toInt for large IDs beyond ~2.1 billion
Sort string numbers without converting first
Assume invalid strings will silently become zero (they error)
Use $toInt when you need decimal fractions
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $toInt
Use these points when converting values to integers in MongoDB.
5
Core concepts
🔢01
To int32
Whole numbers.
Purpose
📝02
{ $toInt: x }
One argument.
Syntax
🔢03
Truncates
3.9 → 3.
Behavior
🛠04
Parse & sum
String to int.
Usage
⚠05
int32 limit
Use $toLong.
Range
❓ Frequently Asked Questions
$toInt converts a value to a 32-bit integer inside an aggregation pipeline. It is an aggregation expression operator used in stages like $project and $addFields.
The syntax is { $toInt: <expression> }. The expression can be a field reference like "$quantity", a numeric string, or another numeric expression.
$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 (range roughly -2.1 billion to 2.1 billion). $toLong produces a 64-bit integer for larger whole numbers. Use $toLong when values exceed the int32 range.
$toInt returns null when the input is null. It does not throw an error for null input.