Int32 (32-bit Integer)
Last updated: 2026-10-09
Int32 (32-bit Integer)
Int32 is a signed 32-bit integer, ranging from -2147483648 to 2147483647. In mongosh a bare number like 42 is a Double, so construct integers explicitly with `Int32(42)`. Use it for ages, counters, and ids that stay small; switch to Long beyond 32-bit range.
| Category | BSON Types |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
Int32(<integer>)
{ age: Int32(30) }
db.users.find({ age: { $gt: Int32(18) } })
Returns:Returns an Int32 BSON integer
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// Int32: construct an integer explicitly
use shop
db.users.insertOne({ name: 'alice', age: Int32(30) })
db.users.find({ age: { $gt: Int32(18) } })
Output:
{ _id: ObjectId('...'), name: 'alice', age: Int32(30) }
📌 Notes
- Bare 42 in mongosh is a Double; use Int32(42) for an integer
- Out-of-range values throw; switch to Long
- $eq won't match mixed Int32 and Double values
🏷️ Related Tags
💡 Related Cases
Documents and BSON: The Cornerstone of MongoDB Data
Inserting Documents: A Detailed Explanation of `insertOne` and `insertMany`
📚 Related Tutorials
❓ FAQ
QInt32 or Long — how to choose?
AStay within ±2.1 billion with Int32; use Long (64-bit) beyond that or for growth headroom.
QWhat type is 42 in mongosh?
AIt is a Double by default. Use Int32(42) or Long(42) for an integer.
QCan Int32 overflow?
AYes — constructing a value beyond 32-bit range errors; switch to Long.