Blog App (MongoDB with Mongoose)
This document outlines the MongoDB database schema for a sample blog application. The schema is designed to be efficient, scalable, and easy to understand. We use Mongoose for defining our data models with TypeScript.
The core of our application revolves around four main collections: User, Post, Category, and Comment.
Installation Guide
To add these schemas to your project, run:
npx servercn-cli add schema blog-app1. User Schema
The User schema stores essential information about blog authors and administrators, including authentication details and profile information.
MVC Path: src/models/user.model.ts
Feature Path: src/modules/user/user.model.ts
Installation
npx servercn-cli add schema blog-app/user2. Post Schema
The Post schema stores blog posts with support for titles, content, excerpts, featured images, and publication status. Includes text search capabilities and like tracking.
MVC Path: src/models/post.model.ts
Feature Path: src/modules/post/post.model.ts
Installation
npx servercn-cli add schema blog-app/post3. Category Schema
The Category schema organizes blog posts into hierarchical categories with names, slugs, and descriptions.
MVC Path: src/models/category.model.ts
Feature Path: src/modules/category/category.model.ts
Installation
npx servercn-cli add schema blog-app/category4. Comment Schema
The Comment schema enables user engagement through comments on blog posts, with support for nested replies and likes.
MVC Path: src/models/comment.model.ts
Feature Path: src/modules/comment/comment.model.ts
Installation
npx servercn-cli add schema blog-app/commentKey Features
Post Status Workflow
Posts support three states:
- Draft: Initial state, not visible to public
- Published: Visible to all users
- Archived: Hidden from main listings but retained
Text Search
The Post collection includes a text index on title and content fields for full-text search capabilities.
Nested Comments
Comments support threaded discussions through the parentComment field, enabling unlimited nesting levels.
Like System
Both posts and comments track likes using arrays of user ObjectIds, with cached counts for performance.
Soft Delete Support
User schema includes fields for soft delete functionality:
isDeleted: Flag to mark deleted usersdeletedAt: Timestamp of deletionreActivateAvailableAt: When reactivation becomes available
Security Features
- Password field excluded by default with
select: false - Account lockout support with
failedLoginAttemptsandlockUntil - Email verification tracking with
isEmailVerified