Skip to the page

NoSQL and MongoDB · Design

Blog posts and comments in MongoDB

The 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

  1. What's the document size limit, and why does it matter here?
  2. How do you show the latest 10 comments on the post page in one read?
  3. An author changes their display name. What do you update?
  4. How do you store likes so you can show a count and 'did I like this'?
  5. 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 likes collection with a unique index on { postId: 1, userId: 1 }, plus a counter on the post updated with $inc.
  • Heavy use of $lookup is 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 voice

Practise 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.

Start your free interview