Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: A Blog Data Layer

Design and build the data layer of a blog with Mongoose - users, posts, comments, slugs, tags, pagination and a tag report - with the real schemas read-only and a runnable in-memory version.

In this project you design the data layer of a small blog: authors, posts with tags, and comments. You will make the modeling decisions from this chapter's lessons (references or embedding, indexes, slugs, pagination, aggregation) and then run a working in-memory version of the same logic.

The real Mongoose code is shown read-only because Mongoose and MongoDB cannot run on this site. The runnable program implements the same functions over arrays, so you can step through the behavior. When you build it for real, the function names and flow stay the same.

Requirements

  • Users have a name and a unique email.
  • Posts have a title, a unique URL slug generated from the title, a body, tags, an author and a published flag.
  • Comments belong to a post and have an author name and text.
  • The home page lists published posts newest first with cursor pagination and shows each author's name.
  • A post page shows the post with its comments.
  • A tag page lists posts with a tag.
  • An admin report shows how many published posts use each tag.

Step 1: Model the data

Decisions, using the rules from Embedding or referencing:

  • Author: a post references the user, and also stores a copy of the author's name (an extended reference) so the home page needs no second query.
  • Comments: unbounded and listed on their own, so a separate collection with a post reference, served by virtual populate.
  • Tags: a small bounded array of strings on the post, indexed (a multikey index).
JavaScript
import mongoose from 'mongoose';
const { Schema, model } = mongoose;

const userSchema = new Schema({
  name: { type: String, required: true, trim: true },
  email: { type: String, required: true, unique: true, lowercase: true },
}, { timestamps: true });

const postSchema = new Schema({
  title: { type: String, required: true, trim: true, maxlength: 140 },
  slug: { type: String, required: true, unique: true },
  body: { type: String, required: true },
  tags: { type: [String], default: [] },
  published: { type: Boolean, default: false },
  author: {
    _id: { type: Schema.Types.ObjectId, ref: 'User', required: true },
    name: String,
  },
}, { timestamps: true, toJSON: { virtuals: true } });

postSchema.index({ published: 1, _id: -1 });   // home page feed
postSchema.index({ tags: 1, published: 1 });   // tag pages and report

postSchema.virtual('comments', { ref: 'Comment', localField: '_id', foreignField: 'post' });

const commentSchema = new Schema({
  post: { type: Schema.Types.ObjectId, ref: 'Post', required: true, index: true },
  authorName: { type: String, required: true },
  text: { type: String, required: true, maxlength: 2000 },
}, { timestamps: true });

export const User = model('User', userSchema);
export const Post = model('Post', postSchema);
export const Comment = model('Comment', commentSchema);

The first index serves the feed (published equality, then newest _id: Equality, Sort from the ESR rule).

Step 2: The operations

JavaScript
export async function createPost(user, { title, body, tags = [] }) {
  const base = slugify(title);
  for (let n = 1; n < 20; n++) {
    const slug = n === 1 ? base : `${base}-${n}`;
    try {
      return await Post.create({ title, body, tags, slug, author: { _id: user._id, name: user.name } });
    } catch (err) {
      if (err.code !== 11000 || !err.keyPattern?.slug) throw err;  // only retry slug clashes
    }
  }
  throw new Error('Could not find a free slug');
}

export function feed({ after = null, limit = 10 } = {}) {
  const filter = { published: true, ...(after && { _id: { $lt: after } }) };
  return Post.find(filter).sort({ _id: -1 }).limit(limit + 1).select('title slug tags author createdAt').lean();
}

export const postPage = (slug) =>
  Post.findOne({ slug, published: true }).populate({ path: 'comments', options: { sort: { _id: 1 } } });

export const tagReport = () =>
  Post.aggregate([
    { $match: { published: true } },
    { $unwind: '$tags' },
    { $group: { _id: '$tags', posts: { $sum: 1 } } },
    { $sort: { posts: -1, _id: 1 } },
  ]);

Look at how each lesson shows up: retrying on error code 11000 (from Indexes in Mongoose), limit + 1 with lean() and select() (Lean, select and pagination), and $unwind plus $group (group and lookup).

Step 3: A runnable in-memory version

The program below implements the same functions over arrays: slug generation with collision handling, the feed with limit + 1 cursor pagination, the post page with comments, and the tag report.

Node.js Blog data layer, in memory (stand-in)

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Step 4: Test it

Cover these with the strategy from Testing Mongoose code:

  • Unit: validation errors for missing title, an over-long title, a malformed email (no database).
  • Integration (memory server): two simultaneous createPost calls with the same title get different slugs; feed returns limit items and a cursor and the second page continues without gaps or duplicates; a deleted user's posts keep their author name.

Step 5: Extend it

  1. Add a text index on title and body and a search(q) function.
  2. Add soft delete with the plugin from Schema plugins and reuse.
  3. Add an updateAuthorName(userId, name) that renames the user and updates the copied name on all their posts with updateMany({ 'author._id': userId }, { 'author.name': name }).
  4. Add a commentCount to the feed using a count: true virtual or an aggregation $lookup.
  5. Store likes as a separate collection with a unique index on (post, user) so a user cannot like twice.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the post store a copy of the author's name?

    An extended reference keeps the id plus a few display fields, trading update cost for read speed.
  2. 2. How does createPost handle two posts with the same title?

    The unique index is the guard against races; the application catches error 11000 and tries the next slug.
  3. 3. What does fetching limit + 1 posts tell you?

    If limit + 1 rows come back, there is a next page; you then return only limit rows and a cursor.
  4. 4. Why are comments in their own collection?

    Unbounded child lists should not be embedded in the parent.

Summary

  • Choose references, extended references and embedding per relation, from how data is read.
  • Unique indexes guard slugs and emails; handle error 11000 and retry.
  • Cursor pagination with limit + 1, lean() and select() keeps the feed fast.
  • Virtual populate provides comments without storing ids on the post.
  • Test validation without a database and the tricky races with a few integration tests.

Next: Project: an order system with stock