Quick revision
The short answer
The post document hit MongoDB's 16 MB limit for a single document, which an unbounded comments array will always reach on a popular post; long before that, every new comment was rewriting and replicating a huge document and bloating the multikey indexes on the array. The fix is to move comments into their own collection with a postId field and an index on {postId: 1, createdAt: -1}, and to keep only a small embedded subset, such as the latest five comments and a count, inside the post for fast page loads. The rule of thumb is to embed data that is bounded, owned and read together, and reference data that is unbounded or accessed on its own.
What to remember
In this answer, we saw that a popular post failed because an unbounded embedded array eventually reaches MongoDB's 16 MB document limit, and that the design was already costing us in rewrites, replication, network reads and multikey index entries. We moved comments into their own collection indexed on postId and createdAt, used keyset pagination, and kept fast page loads by embedding a bounded subset with $slice and a counter. The rule is to embed bounded, owned data read together and reference unbounded or independently accessed data.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The post document hit MongoDB's 16 MB limit for a single document, which an unbounded comments array will always reach on a popular post; long before that, every new comment was rewriting and replicating a huge document and bloating the multikey indexes on the array. The fix is to move comments into their own collection with a postId field and an index on {postId: 1, createdAt: -1}, and to keep only a small embedded subset, such as the latest five comments and a count, inside the post for fast page loads. The rule of thumb is to embed data that is bounded, owned and read together, and reference data that is unbounded or accessed on its own.
Why this question is asked
Embedding is one of MongoDB's best features: a whole blog post with its comments can be read in one query, with no joins. That is why so many teams start by putting comments in an array. The design works perfectly for months and fails exactly when the product succeeds, on the most popular post, which is the worst possible moment. Interviewers ask this to see whether we know the hard limit, understand the performance costs that appear before it, and can remodel the data without losing the fast page load. In this answer, we will recap embedding and the document size limit, see what goes wrong as the array grows, move comments into their own collection, add the subset pattern back for speed, and finish with the rule we can apply to any similar decision.
1A Quick Recap of Embedding and the Document Limit
In MongoDB, a document is a JSON-like record stored in BSON, a binary format. We can embed related data inside a document, as a subdocument or an array of subdocuments, instead of storing it in another collection. Reading the post then returns its comments too, in one round trip, and updating the post and its comments together is atomic because it is one document.
But every document has a hard maximum size of 16 MB. This limit exists so that a single document cannot use excessive memory or bandwidth when it is read, written or replicated. A comment with author, text and timestamps might be 500 bytes, so roughly 30,000 comments fill the document. When an update would make the document bigger than the limit, MongoDB rejects it with an error saying the resulting document is too large, and the new comment is simply lost from the user's point of view. The exact error text varies by version.
2The Costs That Arrive Before the Limit
The limit is the visible failure, but the design was hurting long before it. MongoDB's WiredTiger storage engine writes a modified document as a new version, so appending one 500-byte comment to a 10 MB document means rewriting a large document in the cache and on disk. The change also travels to every secondary through the oplog, the replication log; MongoDB records $push efficiently in the oplog, but the secondaries still apply it to the large document. Meanwhile every page view reads the whole document over the network, even when the page shows only the first 20 comments.
Indexes suffer too. An index on comments.author is a multikey index, which means MongoDB adds one index entry per array element. A post with 30,000 comments has 30,000 entries in that index, all pointing at one document, and every comment added updates them. So the cost of writing, reading and indexing the post all grow with the comment count, which is the opposite of what we want for the hottest document in the system.
3Moving Comments to Their Own Collection
The fix is to reference instead of embed. Each comment becomes its own small document that records which post it belongs to.
Now each new comment is a small insert that touches one small document and one index entry, and the post document stays the same size forever. The compound index on postId and createdAt makes 'the newest 20 comments of this post' a short index scan that already returns rows in order. Notice the keyset pagination in the last query: continuing from the last timestamp stays fast at page 500, whereas skip(10000) would walk over 10,000 index entries. If two comments can share the same createdAt, we add _id as a tie-breaker in both the sort and the condition.
4Keeping Fast Page Loads With the Subset Pattern
We lost one thing: the post page used to load in a single read. We can get most of that back by embedding a bounded subset, the latest few comments plus a counter, which is called the subset pattern.
| Aspect | Comments embedded | Comments referenced plus subset |
|---|---|---|
| Post size | Grows with every comment, up to 16 MB | Fixed: post, count and 5 comments |
| Adding a comment | Rewrites a large document | Small insert plus small update |
| Post page | Reads every comment | One read shows the latest 5 |
| All comments, page 2 | Slice of a huge array | Index scan on postId, createdAt |
| Index on author | One entry per element of one big array | One entry per comment document |
The $slice: -5 modifier trims the array to its last five elements in the same atomic update, so recentComments is bounded by design and can never grow. The page renders the post, the count and five comments from one document, and only users who click 'show all comments' trigger a query on the comments collection. The two writes are not in one transaction, so for a moment the count might lag the comments collection; for a blog that is acceptable, and if it were not, we could wrap both in a multi-document transaction.
5The Rule We Can Reuse
The general rule is to embed data that is bounded, owned by the parent and read together with it, and to reference data that is unbounded or accessed independently. A user's two or three addresses are bounded and always shown with the user, so they are embedded. A post's comments are unbounded and also listed, moderated and searched on their own, so they are referenced. Order lines are bounded by the order and belong to it, so they are embedded.
The useful habit is to ask, for every array we design, what its maximum size is. If the honest answer is 'it depends on how popular this gets', the array must not be embedded without a bound like $slice.
6Follow-up questions
Could GridFS solve the size problem?
No. GridFS splits large files such as images or videos into chunks; it is for binary content, not for records we query and update. Comments are many small records, which is what a collection is for.
Why not bucket comments, say 100 per document?
The bucket pattern is a valid middle ground: documents like {postId, page: 3, comments: [100 items]} reduce document count and keep related comments together. It adds complexity in deciding when a bucket is full, so for a blog the simple one-document-per-comment design is usually enough.
How do we migrate existing posts?
A script reads each post, inserts its comments into the new collection with postId, sets commentCount and recentComments, and then unsets the old comments field. The application is first changed to read from both places and write to the new one, so the migration can run in the background.
7Conclusion
In this answer, we saw that a popular post failed because an unbounded embedded array eventually reaches MongoDB's 16 MB document limit, and that the design was already costing us in rewrites, replication, network reads and multikey index entries. We moved comments into their own collection indexed on postId and createdAt, used keyset pagination, and kept fast page loads by embedding a bounded subset with $slice and a counter. The rule is to embed bounded, owned data read together and reference unbounded or independently accessed data.
Open with
What happened is that the post document reached MongoDB's 16 MB limit for a single document. Comments were an unbounded embedded array, so the most popular post was always going to hit it, and once an update would push it past the limit MongoDB rejects the write.
Then, if they want more
The design was hurting before that, too: every new comment rewrote a large document, secondaries had to apply that change, every page view read all comments over the network, and any index on a comment field was multikey with one entry per comment.
I would remodel it by moving comments into their own collection, one document per comment with a postId, and an index on postId ascending and createdAt descending, so the newest comments of a post are an index scan in order, with keyset pagination for later pages.
To keep the page fast, I would use the subset pattern: the post keeps the latest five comments and a commentCount, maintained with a push using each and slice minus five plus an inc, so it stays bounded.
The general rule I use is to embed data that is bounded, owned and read together, and to reference anything unbounded or accessed on its own.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- MongoDB manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A blog stores every comment inside the post document as an array. A popular post now...": https://scrivio-interview-library.netlify.app/databases/mongodb/87e128fe8a/