Blog with comments (MongoDB) database schema
Authors, posts with embedded tags and SEO fields, comments by reference, and sessions that expire.
4 collections, 4 references
Opens as a new diagram you can change. Free.
Collections
users
Authors and readers.
- _idobjectId not null
- emailstring not null
- namestring not null
- biostring
- rolesarray not null
- createdAtdate not null
posts
A post, with what's always shown alongside it embedded.
- _idobjectId not null
- authorId> usersobjectId not null
- titlestring not null
- slugstring not null
- bodystring not null
- tagsarray
- seoobject
- seo.titlestring
- seo.descriptionstring
- publishedAtdate
comments
Comments by reference: a popular post can gather thousands.
- _idobjectId not null
- postId> postsobjectId not null
- authorId> usersobjectId not null
- bodystring not null
- createdAtdate not null
sessions
Sign-in sessions, removed by a TTL index when they expire.
- _idobjectId not null
- userId> usersobjectId not null
- expiresAtdate not null
As mongosh script (collections, validators, indexes)
The diagram also exports JSON Schema per collection, Mongoose models, Prisma (MongoDB), Sample documents (mongosh), Sample documents (mongoimport, Extended JSON).
// Blog with comments (MongoDB): MongoDB collections, validators and indexes.
// Run with: mongosh "<connection string>" schema.js
// References the application keeps (not enforced by MongoDB):
// posts.authorId → users._id
// comments.postId → posts._id
// comments.authorId → users._id
// sessions.userId → users._id
// users
db.createCollection("users", {
validator: { $jsonSchema: {
"title": "users",
"description": "Authors and readers.",
"bsonType": "object",
"required": [
"_id",
"email",
"name",
"roles",
"createdAt"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"email": {
"bsonType": "string"
},
"name": {
"bsonType": "string"
},
"bio": {
"bsonType": "string"
},
"roles": {
"bsonType": "array"
},
"createdAt": {
"bsonType": "date"
}
}
} },
});
db.getCollection("users").createIndex({
"email": 1
}, {
"name": "email_1",
"unique": true
});
// posts
db.createCollection("posts", {
validator: { $jsonSchema: {
"title": "posts",
"description": "A post, with what's always shown alongside it embedded.",
"bsonType": "object",
"required": [
"_id",
"authorId",
"title",
"slug",
"body"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"authorId": {
"bsonType": "objectId"
},
"title": {
"bsonType": "string"
},
"slug": {
"bsonType": "string"
},
"body": {
"bsonType": "string"
},
"tags": {
"bsonType": "array"
},
"seo": {
"bsonType": "object",
"properties": {
"title": {
"bsonType": "string"
},
"description": {
"bsonType": "string"
}
}
},
"publishedAt": {
"bsonType": "date"
}
}
} },
});
db.getCollection("posts").createIndex({
"slug": 1
}, {
"name": "slug_1",
"unique": true
});
db.getCollection("posts").createIndex({
"publishedAt": -1
}, {
"name": "publishedAt_-1",
"sparse": true
});
// comments
db.createCollection("comments", {
validator: { $jsonSchema: {
"title": "comments",
"description": "Comments by reference: a popular post can gather thousands.",
"bsonType": "object",
"required": [
"_id",
"postId",
"authorId",
"body",
"createdAt"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"postId": {
"bsonType": "objectId"
},
"authorId": {
"bsonType": "objectId"
},
"body": {
"bsonType": "string"
},
"createdAt": {
"bsonType": "date"
}
}
} },
});
db.getCollection("comments").createIndex({
"postId": 1,
"createdAt": 1
}, {
"name": "postId_1_createdAt_1"
});
// sessions
db.createCollection("sessions", {
validator: { $jsonSchema: {
"title": "sessions",
"description": "Sign-in sessions, removed by a TTL index when they expire.",
"bsonType": "object",
"required": [
"_id",
"userId",
"expiresAt"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"userId": {
"bsonType": "objectId"
},
"expiresAt": {
"bsonType": "date"
}
}
} },
});
db.getCollection("sessions").createIndex({
"userId": 1
}, {
"name": "userId_1"
});
db.getCollection("sessions").createIndex({ "expiresAt": 1 }, { name: "expiresAt_ttl", expireAfterSeconds: 0 });
More Content & media templates
- Blog / CMSAuthors, posts, categories (many-to-many) and threaded comments.
- Headless CMSContent types with fields, localised entries, versions and assets.
- News publicationSections, articles with several authors, tags and corrections.
- Podcast hostingShows, episodes, hosts and download stats.
- Video streamingTitles with seasons and episodes, viewer profiles, watch history and ratings.
- Music streamingArtists, albums and tracks, listener playlists and play counts.
- Photo sharingAlbums of photos with tags, likes and comments.
- Wiki / knowledge baseSpaces of nested pages with revisions, attachments and links between pages.
- Email newsletterPublications, subscribers, issues and per-send open and click tracking.
- Digital asset managementAssets in folders with renditions, collections and usage licences.
Start with one diagram, free
One diagram with two branches, no card needed. Upgrade when your team needs more.