NoSQL and MongoDB · Design
Blog posts and comments in MongoDB
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
Model a blog platform in MongoDB: users, posts, comments and likes. A popular post can get 200,000 comments. What do you embed, what do you reference, and why?
Then
Follow-ups the interviewer asks next
- What's the document size limit, and why does it matter here?
- How do you show the latest 10 comments on the post page in one read?
- An author changes their display name. What do you update?
- How do you store likes so you can show a count and 'did I like this'?
- What indexes do you create?
The answer
What a strong answer covers
- MongoDB's own rule: data that is read together is stored together. Model from the screens and queries.
- Embed what belongs to the parent, is read with it and stays small: tags, a copy of the author's name and avatar.
- Reference what grows without limit or is read alone: comments in their own collection with
postId, indexed on{ postId: 1, createdAt: -1 }. - A BSON document can't exceed 16 MB, and huge arrays hurt index performance (MongoDB lists unbounded arrays as an anti-pattern). Patterns: subset (embed the latest 10 comments), bucket (comments in documents of 100), computed (
commentCount,likeCount), extended reference (copied author name). - Copied fields need an update plan: a background job updates the author name, or you accept old names on old comments.
- Likes: a
likescollection with a unique index on{ postId: 1, userId: 1 }, plus a counter on the post updated with$inc. - Heavy use of
$lookupis a sign the model fights the queries.
The red pen
Common traps
Avoid:all comments in an array on the post; normalising like SQL and joining with $lookup everywhere.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.