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

    Embedded Documents

    Embedding is the act of storing related data inside its parent document instead of a separate collection.

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

    Introduction

    Embedding is the act of storing related data inside its parent document instead of a separate collection. It's the most common modeling pattern in MongoDB and the source of most performance wins over SQL.

    Understanding the topic

    Embedding rule of thumb:

    • Data is owned by the parent (address, settings).
    • Always read together.
    • Bounded size (≤ ~MB, ≤ ~hundreds of elements).
    • Doesn't need to be queried alone across parents.

    Syntax reference

    js
    // Embed
    { _id: 1, user: "Ada", profile: { bio: "...", avatar: "..." } }
    // Versus reference
    { _id: 1, user: "Ada", profileId: ObjectId("...") }

    Real-world use

    Order line items are almost always embedded — they're created with the order, read with the order, and immutable after checkout.

    Best practices

    • Embed when ownership + locality + bounded size align.
    • Reference otherwise.
    Ready to mark this lesson complete?Track your journey across the entire course.