Long (64-bit Integer)
Last updated: 2026-10-09
Long (64-bit Integer)
Long is a signed 64-bit integer (BSON type int64), roughly ±9.2×10^18, constructed with `Long(42)` or the alias `NumberLong(42)`. Use it for millisecond timestamps, big counters, and large ids. Values beyond JavaScript's safe integer (about 9×10^15) must be built from strings to avoid precision loss.
| Category | BSON Types |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
Long(<integer>)
Long("<large number string>")
NumberLong("9223372036854775807")
Returns:Returns a Long (int64) BSON integer
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// Long: 64-bit large integer
use shop
db.stats.insertOne({ total: Long('9223372036854775807') })
db.stats.find({ total: { $gt: Long(1000) } })
Output:
{ _id: ObjectId('...'), total: Long('9223372036854775807') }
📌 Notes
- Build values beyond Number.MAX_SAFE_INTEGER from strings
- Type names differ per driver: Long (Java/Node), Int64 (Python)
- Mixing Int32 and Long breaks $type consistency
🏷️ Related Tags
💡 Related Cases
Documents and BSON: The Cornerstone of MongoDB Data
Inserting Documents: A Detailed Explanation of `insertOne` and `insertMany`
📚 Related Tutorials
❓ FAQ
QIs Long the same as NumberLong?
ANo — NumberLong is mongosh's alias for Long; both produce the int64 type.
QHow large can a Long hold?
ASigned 64-bit, about ±9.2×10^18 — far beyond JS's safe integer range.
QWhy construct Long from a string?
ANumeric literals above ~9×10^15 lose precision in JS; string construction stays exact.