Lesson 16 / 22
Schema Example: Blog & Comments
Two ways to model a blog post with its comments.
Embedded comments
Good for posts with a handful of comments, always shown together with the post.
{
_id: 1,
title: "Intro to MongoDB",
body: "...",
comments: [
{ user: "bo", text: "Nice post!" },
{ user: "ivy", text: "Very clear." }
]
}Referenced comments
Better when a post can accumulate thousands of comments — each comment is its own document referencing the post.
// posts collection
{ _id: 1, title: "Intro to MongoDB", body: "..." }
// comments collection
{ postId: 1, user: "bo", text: "Nice post!" }
{ postId: 1, user: "ivy", text: "Very clear." }Trade-off recap
Embedding means one fast read but a document that can grow unbounded. Referencing keeps documents small but needs a second query (or $lookup) to join data.