JSON to BSON Type Mapper
Analyze how MongoDB stores your JSON as BSON. See the exact BSON type, type code, estimated byte size, and MongoDB shell representation for each field.
Features
đ BSON Type Detection
- ObjectId detection (24-char hex)
- ISO date string â BSON Date
- Int32 vs Int64 inference
- Double vs string disambiguation
đ Size Estimation
- Per-field byte size estimate
- Total BSON document size
- BSON overhead calculation
- Optimization guidance
đ§ MongoDB Shell Output
- Ready-to-use insertOne() example
- Correct shell type constructors
- ObjectId(), ISODate(), NumberLong()
- Copy-paste ready
BSON Types Guide
What is BSON?
BSON (Binary JSON) is the binary-encoded serialization format MongoDB uses to store documents. It extends JSON with additional types like ObjectId, Date, Int32, Int64, and Binary.
// JSON string â BSON ObjectId
"_id": "507f1f77bcf86cd799439011"
// MongoDB stores as: ObjectId("507f1f77bcf86cd799439011")
Working with BSON and MongoDB types
Plain JSON cannot express several types MongoDB relies on, such as ObjectId, dates and 64-bit integers. Understanding how these are written in Extended JSON avoids subtle bugs when moving data between tools.
Tips and common pitfalls
- MongoDB Extended JSON writes special types as objects, for example
{"$oid": "..."}and{"$date": "..."}. mongoimport and mongosh understand this format. - Store timestamps as BSON dates rather than strings so range queries and TTL indexes work.
- The BSON document size limit is 16 MB. Split larger data into multiple documents or use GridFS for files.
- Use
Decimal128for money to avoid floating point rounding errors.
More questions
What does an ObjectId contain?
Twelve bytes: a 4-byte timestamp, a 5-byte random value and a 3-byte counter. Because the timestamp comes first, sorting by _id roughly sorts by creation time.
Is BSON smaller than JSON?
Not always. BSON stores field names in every document and adds length prefixes for fast traversal, so it can be larger than compact JSON. Its advantages are speed and richer types.