Skip to content
Tutorials Center
My learning

Topics

Lesson 63 of 70 ยท Projects

Project: Blog with Comments and Tags

Build the data layer of a blog in MongoDB: decide what to embed, query by tags, keep comment counts correct with aggregation, and paginate posts.

A blog looks simple, yet it contains every decision you meet when you model data in MongoDB. Posts have an author, a handful of tags and an unbounded stream of comments. Some of that data should live inside the post document, some should live in its own collection, and a few numbers (like the comment count on the home page) are worth storing twice. In this project you make those decisions on purpose, one access pattern at a time.

You will build the data layer only: documents, queries, updates and aggregation pipelines. There is no web server. All code runs in the tutorial's practice engine (see The Practice Engine Used in This Tutorial), a small JavaScript stand-in, so the filters, updates and pipelines are real MongoDB syntax but the engine is not MongoDB. If you have not yet read Embedding vs Referencing, skim it first: this project applies it.

What the blog must answer

Home page Latest posts with an author name and a comment count.
Tag pages Posts by one tag, or by several at once.
Comments Add a comment and read a post's thread.
Pagination Page through posts without slow skips.
1 / 2

Step 1: decide what to embed

Start from the reads, not from the entities. The home page shows a post title, the author's display name and a comment count. If the author's name came from a separate authors collection, every list view would need a join. Instead the post stores a small copy of the author: { id, name }. This is the extended reference pattern: you pay a little duplication (rename an author and you update their posts) to make the most frequent read a single-document read.

Tags are the opposite case. A post has maybe three to eight of them, they are always shown with the post, and the main query is "which posts have this tag". A plain array of strings inside the post is perfect, and MongoDB can index an array field so that tag lookups are fast (an index on an array is called a multikey index, see Index Types).

Comments are different again. A popular post may collect thousands, and a document is capped at 16 MB, so an array of comments inside the post would grow without a bound. Comments therefore get their own collection, each with a postId. The cost is a second query (or a $lookup) when you show a thread. To keep the home page cheap, the post keeps a stored commentCount that you update with $inc each time a comment is added: the computed pattern from Schema Design Patterns.

The first TryIt creates the data and answers the tag questions. Pay attention to the three ways of asking about tags (one tag, all of several, any of several) and to the tag cloud, which needs an aggregation because it counts across array elements.

JS Blog data and tag queries

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In mongosh the same queries read almost word for word, with db.posts in front of find:

JavaScript
db.posts.find({ tags: { $all: ["mongodb", "beginner"] }, status: "published" })
db.posts.createIndex({ tags: 1, publishedAt: -1 })

That compound index (a multikey index on tags, then publishedAt) serves the tag page: equality on the tag, then a sort by date without an in-memory sort. Note that the filter also checks status; in a real application you would likely add status to the index or, better, use a partial index that only covers published posts.

Step 2: comments and the stored count

Adding a comment is two writes: insert the comment, then $inc the post's counter. MongoDB makes each single-document write atomic, but the pair is not atomic unless you use a transaction (see Multi-Document Transactions). For a comment count that is usually acceptable. The count can drift if the application crashes between the two writes, so the design needs a repair path: an aggregation that recomputes the true count and a job that writes it back.

The second TryIt shows both sides. addComment does the two writes. Then we deliberately "break" the counter on one post (as a crash might), compare the stored count with a $lookup plus $size computation, and repair it. Because the comments live in their own collection, you also see the read for a thread: a plain find on postId, sorted by time.

JS Comments, counters and a repair job

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the audit: only post 3 shows DRIFT, and the repair's modifiedCount is 1. The pipeline is the "source of truth" and the stored number is a cache. This is the general shape of every computed value: write it cheaply on the hot path, verify it with an aggregation, and keep a way to rebuild it.

A real deployment indexes comments on { postId: 1, createdAt: 1 }, so loading a thread is an index range scan. Remember that $lookup on a large comments collection without that index is a full scan per post (see Joining with $lookup).

Step 3: pagination

The home page shows ten posts per page. The obvious tool is skip and limit: page 3 is skip(20).limit(10). It is simple and fine for the first few pages, but the server still walks past every skipped document, so deep pages get slower (see Sorting, Limiting and Skipping). A blog rarely has a page 500, but the same code often ends up behind an API where someone does request it.

The better habit is range-based (keyset) pagination: remember the sort key of the last post you showed and ask for the posts after it. With newest-first ordering the next page is "posts published before this date". If two posts could share a timestamp, add _id as a tiebreaker in both the sort and the filter. The third TryIt compares the two approaches and proves they return the same pages.

JS Offset pages versus keyset pages

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Both loops print the same three pages (#12 down to #8, #7 down to #3, then #2 and #1). The difference is invisible here with twelve documents and decisive with twelve million: the keyset query starts from an index position instead of counting past the skipped rows. The trade-off is that keyset pages cannot jump straight to "page 40"; they only move forward or back from a known position, which suits "load more" buttons and infinite scroll.

What to take from the project

  • Embed small, bounded, read-together data (tags, an author summary). Reference data that grows without a limit (comments).
  • Duplicate on purpose when it removes a join from the most common read, and know how you would update the copies.
  • Stored counters are caches. Maintain them with $inc, audit them with $lookup and $size, repair them with $set.
  • Use keyset pagination for anything that can grow large.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the blog store comments in their own collection instead of an array inside each post?

    Unbounded growth is the classic reason to reference instead of embed. Tags are short and bounded, so they stay embedded.
  2. 2. Which filter finds posts that have BOTH the tags mongodb and beginner?

    $all requires every listed value to be present in the array. $in matches any one of them, and an exact array match also demands the same order and no other tags.
  3. 3. A stored commentCount no longer matches the real number of comments. What is the right fix in this design?

    A stored counter is a cache of a computed value. The aggregation is the source of truth, and a repair job rewrites the counter.
  4. 4. What is the main advantage of keyset pagination over skip and limit?

    skip makes the server walk past all skipped documents. A range condition on the sort key starts where the previous page ended.

Summary

  • Design from the reads: the home page, tag pages, threads and pagination decided every embedding choice.
  • Authors are copied into posts (extended reference), tags are an embedded array, comments are a separate collection.
  • $all, $in and $unwind plus $group cover tag queries and a tag cloud.
  • A commentCount maintained with $inc is fast to read but can drift, so keep an aggregation that audits and repairs it.
  • Prefer range-based pagination with a tiebreaker over large skip values.

Next, turn raw orders into reports in Project: Sales Analytics Pipelines.