The Document Model
At the heart of MongoDB is the document model. Every piece of data you store is a document encoded in BSON (Binary JSON) — a binary-encoded format that extends JSON with additional data types and is optimized for speed and storage efficiency.
1. BSON vs JSON
BSON supports everything JSON supports, plus richer types that JSON lacks natively — dates, binary data, and precise numeric types.
| BSON Type | Description | Example |
|---|---|---|
String | UTF-8 text | "Hello World" |
Int32 / Int64 | Whole numbers | 42 |
Double | Floating point numbers | 19.99 |
Boolean | true / false | true |
Date | Milliseconds since Unix epoch | ISODate("2026-07-18") |
ObjectId | 12-byte unique identifier | ObjectId("64f1a2...") |
Array | Ordered list of values | ["mongo", "sql"] |
Object (Embedded Document) | Nested document | { "city": "Delhi" } |
Null | No value | null |
Binary Data | Raw binary (images, files) | BinData(...) |
2. The ObjectId
Every document has a unique _id field. If you don't provide one, MongoDB auto-generates an ObjectId — a 12-byte value encoding a timestamp, machine identifier, process ID, and counter.
ObjectId("64f1a2b3c4d5e6f7a8b9c0d1")
// 4 bytes timestamp | 5 bytes random value | 3 bytes incrementing counter
3. Embedded Documents
Related data can be nested directly inside a parent document — ideal for "contains" relationships that are usually queried together.
{
"_id": ObjectId("..."),
"name": "Rohan Verma",
"address": {
"street": "12 MG Road",
"city": "Pune",
"zip": "411001"
}
}
4. Arrays
Arrays let a single field hold multiple values — including multiple embedded documents.
{
"_id": ObjectId("..."),
"title": "MongoDB Fundamentals",
"tags": ["database", "nosql", "backend"],
"reviews": [
{ "user": "Priya", "rating": 5 },
{ "user": "Karan", "rating": 4 }
]
}
5. Document Model Checklist
- ✅ Understand BSON's extra types — Date, ObjectId, Binary, Int32/64
- ✅ Know when to embed — tightly related, frequently accessed together data
- ✅ Know the 16MB document size limit
- ✅ Comfortable reading nested field and array syntax
Ready to master MongoDB?
Build real-world MongoDB-powered applications with hands-on projects, mentor-led sessions, and placement support.