An early schema design with deeply nested arrays and unbounded document growth worked fine on small data, but caused document size limit errors and slow queries once collections scaled up.

Redesign the schema around actual query patterns - split unbounded arrays into their own collection, and only denormalize data that's genuinely read together often.

Step-by-Step Guide

1

Identify unbounded fields (like comments embedded inside a post) and move them to a separate referenced collection

2

Check for documents approaching MongoDB's 16MB document size limit

3

Design around query patterns: embed data that's always read together, reference data queried independently

4

Add indexes to support the new reference-based queries

5

Use the $lookup aggregation stage for joins where references replace embedding

6

Migrate existing data in small batches to avoid locking the whole collection

7

Benchmark before and after the redesign with realistic data volumes

8

Avoid nesting structures more than 2-3 levels deep since they become hard to query and update

Found an issue with this solution?

Related Topics

mongodb document size limitembedding vs referencing strategymongodb aggregation lookup performance