In this lesson, you will learn how to prevent document bloat and performance degradation by refactoring unbounded arrays into secondary collections.
The Problem: When Arrays Grow Without Bound
In document databases, embedding arrays (like tags or recent_activity) is convenient for read performance. However, if an array grows without a predictable limit—such as an audit log or a history of user actions—your document will eventually exceed the storage engine's maximum document size (often 16MB).
Even before hitting the size limit, unbounded arrays cause significant performance issues. Every time you append a new element, the database must move the entire document to a new location on disk because its size has changed. This leads to massive storage fragmentation and slow write throughput.
The Solution: Moving to a Secondary Collection
To solve this, we refactor the design by "decoupling" the unbounded array from the parent document. We take the items that were previously embedded and store them in a separate collection, referencing the parent via a foreign key (the parent_id).
Before (The Bottleneck):
{
"_id": "user_123",
"name": "Alice",
"audit_logs": ["login", "click", "update", "upload", ...] // Grows indefinitely
}
After (The Optimized Approach): Parent Collection (User Profile):
{ "_id": "user_123", "name": "Alice" }
Secondary Collection (Audit Logs):
{ "user_id": "user_123", "action": "login", "timestamp": "..." }
{ "user_id": "user_123", "action": "click", "timestamp": "..." }
When to Refactor
You should consider this pattern if you notice an array length that scales linearly with the age of the record. By moving these entries into their own collection, you ensure that the parent document remains compact and stable. You can still retrieve the logs by querying the secondary collection using the user_id index. This strategy trades a minor increase in query complexity for much higher write efficiency and system stability.
Key Takeaway: Never embed arrays that grow indefinitely; instead, refactor them into a separate collection and link them via a foreign key to maintain document size stability.