Code companion for the article How to Design Nested Documents for a Blogging App in MongoDB.
Each example file maps directly to a section of the article and can be read alongside it. The repo also ships a small Express REST API that puts the final recommended schema into practice.
- Node.js 18+
- A MongoDB instance — local (
mongodb://localhost:27017) or Atlas - pnpm (or npm/yarn)
-
Install dependencies (from the repo root):
pnpm install
-
Configure environment variables:
cp .env.example .env
Edit
.envand fill in your connection string:Variable Default Description MONGODB_URImongodb://localhost:27017MongoDB connection string MONGODB_DBblogging_app_examplesDatabase name used by examples and API PORT3000Port for the Express server
All scripts are run from the repo root.
node src/examples/02-embed-vs-reference.jsReplace the filename with whichever example you want to explore. Files marked as snippets below are not executable — they exist purely as annotated code for the article.
Must be run before seeding or using the API:
node src/examples/10-final-schema/schema.jsPopulates users, posts, and comments collections so you can explore the final schema and hit the API with real data:
node src/examples/seed.jsThe Express app exposes a REST API backed by the final schema and includes Swagger docs.
Development (auto-restarts on file changes):
pnpm devProduction:
pnpm startOnce running:
| Endpoint | Description |
|---|---|
GET /api/posts |
List posts |
GET /api/posts/:id |
Get a post |
POST /api/posts |
Create a post |
GET /api/users |
List users |
GET /api/users/:id |
Get a user |
GET /api/:postId/comments |
List comments for a post |
POST /api/:postId/comments |
Create a comment |
GET /api-docs |
Swagger UI |
| File | Article section | Runnable |
|---|---|---|
01-entities.js |
What entities exist in a blogging application? | No (snippets) |
02-embed-vs-reference.js |
When should you embed documents? | Yes |
03-blog-post-structure.js |
How should a blog post document be structured? | Yes |
04-comments-embedded.js |
Should comments be embedded inside blog posts? | Yes |
05-comments-separate-collection.js |
Unbounded comments — Option 1: separate collection | Yes |
06-comments-hybrid.js |
Unbounded comments — Option 2: hybrid | Yes |
07-threaded-replies.js |
How do nested replies affect schema design? | Yes |
08-optimization.js |
How do you optimize nested documents for performance? | Yes |
09-common-mistakes.js |
What are common mistakes with nested documents? | No (snippets) |
10-final-schema/ |
Final recommended schema + index setup | Yes |
seed.js |
Sample data for the final schema | Yes |