Sign in
Log inSign up
Mongoose Schema's

Photo by Markus Winkler on Unsplash

Mongoose Schema's

Quick Reference Guide - Copy&Paste To Your Hearts Content

Cruz To A Million's photo
Cruz To A Million
·May 31, 2022·

5 min read

Quick HowTo Reference


As I'm creating my database schema's I occassionly look at previous work when creating databases. I made myself and you, a quick cheatsheet of schema's ready to go! Since I didnt find anything worthy online. These are for me. If you like me to add more to the list, let me know! Follow me if you found this useful! Thanks!


Table of Contents

  1. User Schema
  2. Video Schema
  3. Category Schema
  4. Chat Schema
  5. Featured Schema
  6. Review Schema
  7. Comment Schema
  8. Episode Schema
  9. Podcast Schema
  10. Story Schema

Category Schema

const mongoose = require('mongoose');

const categorySchema = new mongoose.Schema(
    {
        title: {
            type: String,
            required: true,
            unique: true,
        },
        createdBy: {
            type: Schema.Types.ObjectId,
            ref: 'User',
        },      
    },
    {
        timestamps: true
    }
);

const Category = new mongoose.model('CATEGORY', categorySchema);
model.exports = Category;

Chat Schema

const mongoose = require('mongoose');

const chatSchema = new mongoose.Schema(
   {
    messages: [{
          text: { 
            type: String,
            max: 2000
          },
          sender: { 
            type: mongoose.Schema.Types.ObjectId, 
            ref: 'User',
          },
        }],
    participantOne: [{                
            type: mongoose.Schema.Types.ObjectId, 
            ref: 'User',
          }],
    participantTwo: [{                
            type: mongoose.Schema.Types.ObjectId, 
            ref: 'User',
          }],
  },
    {
      timestamp: true
    },
  );

  const Chat = new mongoose.model('CHAT', chatSchema);
  model.exports = Chat;

Comment Schema

const mongoose = require('mongoose');

const commentSchema = new mongoose.Schema(
    {
        body: {
            type: String,
            required: true,
        },
        story: [{
            type: Schema.Types.ObjectId,
            ref: 'Story',
        }[, 
        createdBy: [{
            type: Schema.Types.ObjectId,
            ref: 'User',
        }],        
    },
    {
        timestamps: true
    },
);

const Comment = new mongoose.model('COMMENT', commentSchema);
model.exports = Comment;

Episode Schema

const mongoose = require('mongoose');

const episodeSchema = new mongoose.Schema(
    {
        title: {
            type: String,
            required: true,
        },
        description: {
            type: String,
            required: true,
        },
        language: {
            type: String,
            required: true,
        },
        Url: {
            type: URL,
            required: true,
        },
        slug: {
            type: String,
            required: true,
        },
        rssFeed: {
            type: String,
            required: true,
        },
        episodes: [{
            type: Schema.Types.ObjectId,
            ref: 'Episode'
        }],
        meta: {
            type: String,
            required: true,
        },
        storyPosts: [{
            type: Schema.Types.ObjectId,
            ref: 'Story',
        }],
        featuredPosts: [{
            type: Schema.Types.ObjectId,
            ref: 'Featured',
        }],
        publicationDate: {
            type: Date,
            default: Date.now,
        },
    },
    {
        timestamps: true
    },
);

const Episode = new mongoose.model('EPISODE', episodeSchema);
model.exports = Episode;

const mongoose = require('mongoose');

const featuredSchema = new mongoose.Schema(
    {
        category: {
            type: Schema.Types.ObjectId,
            ref: 'Category',
        },      
        title: {
            type: String,
            required: true,
        },
        imageUrl: {
            type: String,
            required: true,
        },
        slug: {
            type: String,
            required: true,
            unique: true,
        },
        body: {
            type: String,
            required: true,
        },
        viewsCount: {
            type: Number,
            default: 0,
        },
        comments: [{
            type: Schema.Types.ObjectId,
            ref: 'Comment',
        }],
        createdBy: [{
            type: Schema.Types.ObjectId,
            ref: 'User',
        }],        
    },
    {
        timestamps: true
    },
);

const Featured = new mongoose.model('FEATURED', featuredSchema);
model.exports = Featured;

Podcast Schema

const mongoose = require('mongoose');

const podcastSchema = new mongoose.Schema(
    {
        title: {
            type: String,
            required: true,
        },
        description: {
            type: String,
            required: true,
        },
        language: {
            type: String,
            required: true,
        },
        Url: {
            type: URL,
            required: true,
        },
        slug: {
            type: String,
            required: true,
        },
        rssFeed: {
            type: String,
            required: true,
        },
        episodes: [{
            type: Schema.Types.ObjectId,
            ref: 'Episode',
        }],
        meta: {
            type: String,
            required: true,
        },
        storyPosts: [{
            type: Schema.Types.ObjectId,
            ref: 'Story',
        }],
        featuredPosts: [{
            type: Schema.Types.ObjectId,
            ref: 'Featured',
        }],
        publicationDate: {
            type: Date,
            default: Date.now,,,
        },
    },
    {
        timestamps: true
    },
);

const Podcast = new mongoose.model('PODCAST', podcastSchema);
model.exports = Podcast;

Review Schema

const mongoose = require('mongoose');

const reviewSchema = new mongoose.Schema(
    { 
        name: {
            type: String,
            required: true,
        },
        review: {
            type: String,
            required: true,
        },
        rating: {
            type: Number,
            min: 0,
            max: 5,
            required: true,
        },
        createdAt: {
            type: Date,
            defauult: Date.now,
        },
        helpful: {
            type: Boolean,
        }        
    },
    {
        timestamps: true
    },
);

const Review = new mongoose.model('REVIEW', reviewSchema);
model.exports = Review;

Story Schema

const mongoose = require('mongoose');

const storySchema = new mongoose.Schema(
    {
        category: [{
            type: Schema.Types.ObjectId,
            ref: 'Category',
        }],      
        title: {
            type: String,
            required: true,
        },
        imageUrl: {
            type: String,
            required: true,
        },
        slug: {
            type: String,
            required: true,
            unique: true,
        },
        body: {
            type: String,
            required: true,
        },
        viewsCount: {
            type: Number,
            default: 0,
        },
        comments: [{
            type: Schema.Types.ObjectId,
            body: 'String',
            ref: 'Comment',
        }],
        createdBy: [{
            type: Schema.Types.ObjectId,
            ref: 'User',
        }],        
    },
    {
        timestamps: true
    },
);

const Story = new mongoose.model('STORY', storySchema);
model.exports = Story;

User Schema

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema(
    {
        username: {
            type: String,
            required: true,
        },
        name: {
            type: String,
            required: true,
        },
        email: {
            type: String,
            required: true,
        },
        imgage: {
            type: String,
            required: true,
        },
        meta: {
            type: String,
            required: true,
        },
        role: {
            type: String,
            required: true,
            default: 'user',
            enum: ['user', 'admin', 'superAdmin'],
        },
        storyPosts: [{
            type: Schema.Types.ObjectId,
            ref: 'Story',
        }],
        featuredPosts: [{
            type: Schema.Types.ObjectId,
            ref: 'Featured',
        }],
        twitter: {
            type: String,
            url: 'URL',
        },
        facebook: {
            type: String,
            url: 'URL',
        },
        instagram: {
            type: String,
            url: 'URL',
        },
        chats: [{
            type: Schema.Types.ObjectId,
            ref: 'Chat',
        }],
        messages: [{
            type: Schema.Types.ObjectId,
            ref: 'Message',
        }],
        podcast: [{
            type: Schema.Types.ObjectId,
            ref: 'Podcast',
        }],
        password: {
            type: String,
            required: true,
            minlength: 8,
            maxlength: 32,
            unique: true,
        },
        verificationCode: {
            type: Number,
        },
        isEmailVerified: {
            type: Boolean,
            default: false,
        },
        passwordResetCode: {
            type: String,
        },
    },
    {
        timestamps: true
    },
);

const User = new mongoose.model('USER', userSchema);
model.exports = User;

Video Schema

const mongoose = require('mongoose');

const videoSchema = new mongoose.Schema(
    {
        videoId: {
            type: String,
            required: true,
        },
        title: {
            type: String,
            required: true,
        }, 
        createdBy: [{
            type: Schema.Types.ObjectId,
            ref: 'User',
        }],
        viewsCount: {
            type: Number,
            default: 0,
        },        
    },
    {
        timestamps: true
    },
);

const Video = new mongoose.model('VIDEO', videoSchema);
model.exports = Video;

If you would like me to add one, please leave a comment or message me! Thanks.