Course contentsShow
← Databases & SQL
Lesson 1197 of 1,95311. NoSQL and Document ModelsFree lesson

When to Use Document Referencing

Identify scenarios where data normalization is necessary to prevent unbounded document growth.

In this lesson, you will learn how to identify when a document's growth necessitates moving from an embedded model to a referenced model to maintain system performance and storage efficiency.

The Problem of Unbounded Growth

In document databases, embedding data directly into a single document is often a great way to optimize for read-heavy workloads because it keeps related information in one place. However, if you embed arrays that can grow indefinitely—such as a list of logs, user activity streams, or comments on a popular post—you eventually hit the physical limit of the document size.

Most document databases, such as MongoDB, have a strict maximum document size (e.g., 16MB). Even if you don't hit that hard limit, bloated documents increase memory pressure on the database and slow down every operation that touches them, because the entire document must be loaded into RAM.

When to Pivot to Referencing

You should move from an embedded model to a referenced model when you encounter unbounded growth. If the number of items in a sub-document grows in proportion to your user's activity or time, that data belongs in a separate collection.

Consider a "User Profile" document:

// Avoid this: Unbounded growth in an array
{
  "_id": "user123",
  "username": "jdoe",
  "activity_logs": [
    { "timestamp": "...", "action": "login" },
    { "timestamp": "...", "action": "update_profile" },
    // Imagine this growing to thousands of entries...
  ]
}

As activity_logs grows, the user123 document becomes increasingly expensive to move or update. Instead, you should normalize the data by moving logs into their own collection and referencing the user by ID:

// Better: Reference the parent ID in the child collection
{
  "_id": "log999",
  "user_id": "user123",
  "action": "login",
  "timestamp": "..."
}

By referencing, you decouple the growth of the log stream from the profile document. This keeps your primary documents lightweight and prevents performance degradation caused by massive, ever-expanding arrays. When you need to retrieve the activity, you simply perform a query against the logs collection using the user_id.

Key Takeaway: Use document referencing when data is unbounded or continuously growing to prevent document bloat, ensure predictable performance, and stay within storage constraints.