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
Identify unbounded fields (like comments embedded inside a post) and move them to a separate referenced collection
Check for documents approaching MongoDB's 16MB document size limit
Design around query patterns: embed data that's always read together, reference data queried independently
Add indexes to support the new reference-based queries
Use the $lookup aggregation stage for joins where references replace embedding
Migrate existing data in small batches to avoid locking the whole collection
Benchmark before and after the redesign with realistic data volumes
Avoid nesting structures more than 2-3 levels deep since they become hard to query and update
Found an issue with this solution?