The $toLong operator converts values to a 64-bit long integer in MongoDB aggregation pipelines. Use it for large user IDs, Unix millisecond timestamps, and any whole number that exceeds the 32-bit int range.
01
Long Conversion
Values to int64.
02
Syntax
One expression.
03
Large Numbers
Beyond int32 range.
04
$project Stage
Parse big IDs.
05
Use Cases
Timestamps, IDs.
06
vs $toInt
64-bit vs 32-bit.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $toLong operator converts an expression to a 64-bit signed long integer. While $toInt handles values up to about 2.1 billion, many real-world datasets store larger numbers — snowflake IDs, Unix timestamps in milliseconds, and high-volume counters all need the wider range that $toLong provides.
$toLong is shorthand for { $convert: { input: <expression>, to: "long" } }. Like $toInt, it truncates doubles toward zero when converting fractional numbers to whole numbers.
💡
Beginner Tip
JavaScript Date.now() returns milliseconds like 1718448600000 — too large for int32. Use $toLong for these timestamp values.
$toLong errors on invalid strings. $convert with onError lets the pipeline continue with a safe default.
Applications
🚀 Use Cases
Snowflake and distributed IDs — parse large unique identifiers stored as strings.
Unix millisecond timestamps — convert Date.now() values for sorting and comparison.
High-volume counters — sum page views, event counts, and metrics beyond int32 range.
Data import cleanup — normalize large numeric strings from CSV or JSON imports.
Cross-system ID mapping — convert external system IDs that exceed 32-bit limits.
🧠 How $toLong 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 int64
Strings are parsed; doubles and decimals are truncated toward zero; booleans become 0 or 1.
Convert
3
A long integer is returned
The output supports $add, $sum, sorting, and comparisons on large whole numbers.
Output
=
🔢
Large whole number ready
Use for big IDs, timestamps, counters, and int64 arithmetic.
Wrap Up
Conclusion
The $toLong operator converts values to 64-bit long integers in MongoDB aggregation pipelines. It is essential for snowflake IDs, Unix millisecond timestamps, and any whole number that exceeds the 32-bit int range.
For smaller whole numbers, $toInt is sufficient. For decimal fractions, use $toDouble. For conversions with error handling, use $convert. Next in the series: $toLower.
Choose long for snowflake IDs and large external identifiers
Convert before sorting string numbers or timestamps
Use $convert with onError for messy imported data
Prefer $toInt when values fit in the 32-bit range
❌ Don’t
Use $toInt for values above ~2.1 billion
Sort large numeric strings without converting first
Assume invalid strings will silently become zero (they error)
Use $toLong when you need decimal fractions
Forget that doubles are truncated toward zero, not rounded
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $toLong
Use these points when converting values to long integers in MongoDB.
5
Core concepts
🔢01
To int64
64-bit long.
Purpose
📝02
{ $toLong: x }
One argument.
Syntax
🔢03
Big numbers
IDs, timestamps.
Input
🛠04
Sort & sum
Parse then use.
Usage
⚠05
vs $toInt
64 vs 32 bit.
Range
❓ Frequently Asked Questions
$toLong converts a value to a 64-bit long integer inside an aggregation pipeline. It is an aggregation expression operator used in stages like $project and $addFields.
The syntax is { $toLong: <expression> }. The expression can be a field reference like "$userId", a numeric string, or another numeric expression.
Use $toLong when values exceed the 32-bit int range (roughly -2.1 billion to 2.1 billion). Common cases include large user IDs, Unix millisecond timestamps, and big counters.
$toLong is shorthand for { $convert: { input: <expr>, to: "long" } }. Use $convert with onError when invalid values should return a fallback instead of failing the pipeline.
$toLong returns null when the input is null. It does not throw an error for null input.