MongoDB โ Documents & BSON Data Types
Understand the BSON specification, document structures, array fields, embedded sub-documents, and the 16 MB document size limit.
1What is BSON?
While developers write queries using JSON syntax, MongoDB stores documents internally as BSON (Binary JSON). BSON was designed to achieve three key benefits: fast serialisation/deserialisation, compact binary storage, and support for rich data types missing in standard JSON.
2Complete BSON Data Types Reference
| BSON Type | Type Number | Description & Example |
|---|---|---|
| String | 2 | UTF-8 encoded string: "Hello World" |
| Integer (32-bit) | 16 | Standard integer: NumberInt(42) |
| Long (64-bit) | 18 | 64-bit integer for large counts: NumberLong(9007199254740991) |
| Decimal128 | 19 | High-precision decimal for currency: NumberDecimal("99.95") |
| Double | 1 | 64-bit IEEE 754 floating point number: 3.14159 |
| Boolean | 8 | true or false |
| Date | 9 | 64-bit UTC timestamp: ISODate("2026-08-26T12:00:00Z") |
| ObjectId | 7 | 12-byte primary key identifier: ObjectId("...") |
| Array | 4 | Ordered list of elements: ["apple", "banana"] |
| Object | 3 | Embedded sub-document: { city: "Hyderabad" } |
| Null | 10 | Null or missing value: null |
| Binary Data | 5 | Raw binary blobs (UUIDs, images): BinData(...) |
3The 16 MB Document Size Limit
In MongoDB, a single BSON document cannot exceed 16 Megabytes in size. This design limit serves two critical purposes:
- Prevents developers from creating massive unbounded documents that saturate network RAM.
- Ensures fast memory lookup times in WiredTiger cache.
โ ๏ธ Schema Tip:
If you need to store files larger than 16 MB (such as videos or PDFs), use GridFS instead of embedding raw binary data directly into documents!