Sikau

MongoDB · Day 10 · about 40 min

Project: Blog with comments

Model, index and query a small blog.

Posts, authors, comments

Apply this unit's design rules: authors referenced, tags embedded, comments in their own collection (unbounded), with indexes for the queries the blog runs.

Model the blog's queries

This is the design process in miniature: list the pages, write their queries, then shape and index the data to serve them.

List what the blog shows. The home page lists recent posts with their tags and author names. A post page shows the post and its newest comments. An author page lists their posts. Each of those becomes a query, and the queries decide the shape: tags embedded in posts, the author referenced by id, comments in their own collection because they grow without limit.

  • users — { _id, name }.
  • posts — { _id, title, author, tags: [...], publishedAt }.
  • comments — { post, by, text, at }, one document per comment.
  • Indexes: posts { publishedAt: -1 }, posts { author: 1 }, comments { post: 1, at: -1 }.

Copying the author's name into each post saves a lookup on the home page; update it if an author renames.

The rest of this lesson

  • Comments, newest firstexplain
  • Posts by tagexplain
  • Design decisionmatch
  • Build orderorder
  • The blogchallenge
  • Badge quizquiz
  • Unit 2 complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 15 lessons of MongoDB