MongoDB Tutorial 0/120 lessons ~6 min read Lesson 16

    Document Structure

    Every MongoDB document is a tree of key-value pairs.

    Course progress0%
    Focus
    6 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Every MongoDB document is a tree of key-value pairs. Keys are strings, values are any BSON type — including arrays and other documents. Mastering document structure is the foundation of every data model decision you'll ever make.

    Understanding the topic

    The building blocks:

    • Primitive fields — strings, numbers, booleans, dates.
    • Arrays — ordered lists, queryable with operators like $elemMatch.
    • Embedded documents — nested objects, dot-path addressable.
    • Mixed types — the same field can hold different types across documents (avoid in practice).
    • Reserved _id — primary key, immutable once set.

    Syntax reference

    js
    {
    _id: ObjectId("..."),
    name: "Ada",
    age: 36,
    active: true,
    tags: ["admin", "engineer"],
    address: { city: "London", postcode: "EC1A 1AA" },
    meta: { lastLogin: ISODate("2026-06-01") }
    }

    Real-world use

    A well-structured document maps 1:1 to your API response — no joins, no transformations in the application layer.

    Best practices

    • Keep field names short — they're stored on every document.
    • Avoid mixed types per field — kills query optimization.

    Common mistakes

    • Using deeply nested structures (>4 levels) — hard to query and update.
    Ready to mark this lesson complete?Track your journey across the entire course.