BSON Data Types, Embedded Documents, and Arrays

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 TypeDescriptionExample
StringUTF-8 text"Hello World"
Int32 / Int64Whole numbers42
DoubleFloating point numbers19.99
Booleantrue / falsetrue
DateMilliseconds since Unix epochISODate("2026-07-18")
ObjectId12-byte unique identifierObjectId("64f1a2...")
ArrayOrdered list of values["mongo", "sql"]
Object (Embedded Document)Nested document{ "city": "Delhi" }
NullNo valuenull
Binary DataRaw 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 }
  ]
}
Common Issue: Documents have a hard 16MB size limit. Extremely large arrays (e.g., thousands of embedded reviews) should typically be referenced in a separate collection instead.

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
Key Takeaway: Mastering BSON types, embedding, and arrays is foundational — nearly every later topic, from schema design to aggregation, builds on this document model.

Ready to master MongoDB?

Build real-world MongoDB-powered applications with hands-on projects, mentor-led sessions, and placement support.

Explore Course