8

Im trying MongoDB and as a matter of starting point im creating a schema for a chat application that may be simple to scale in the future, so im wondering if this looks correct from what i have been seeing in the docs. So far i have 3 collections User, Room, Message. Also i would need to perform some queries like getting all messages from a sepecific room, get all messages from a specific user, etc

Designed with mongoose:

var user = new mongoose.Schema({
     username: { type: String, lowercase: true, unique: true },
     email: { type: String, lowercase: true, unique: true },
     password: String,
     is_active: { type: Boolean, default: false },
});

var room = new mongoose.Schema({
    name: { type: String, lowercase: true, unique: true },
    topic: String,
    users: [user],
    messages: [message],
    created_at: Date,
    updated_at: { type: Date, default: Date.now },
});

var message = new mongoose.Schema({
    room: room,
    user: user,
    message_line: String,
    created_at: { type: Date, default: Date.now },
});

var User = mongoose.model('User', user);
var Room = mongoose.model('Room', room);
var Message = mongoose.model('Message', message);
Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
Andrés Da Viá
  • 586
  • 1
  • 8
  • 24
  • HI, since this post asked 6+ years, Did you have an issue with this method? It looks good to me, hope you answer your question to share the exprience :D – Tokenyet May 13 '21 at 07:41

1 Answers1

1
//message model
'use strict';

import mongoose from 'mongoose';
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;

var MessageSchema = new Schema({
    send: {
        type: ObjectId,
        ref: 'User',
        required: true
    },

    message: {
        type: String,
        required: true
    },
    date: {
        type: Date
    },
    created_by: {
        type: ObjectId,
        ref: 'User',
        required: true
    },
    thread: {
        type: ObjectId,
        ref: 'MsgThread',
        required: true
    },
    is_deleted: [{
        type: ObjectId,
        ref: 'User'
    }]
}, {
    timestamps: {
        createdAt: 'created_at',
        updatedAt: 'last_updated_at'
    }
});

export default mongoose.model('Message', MessageSchema);

//dont use rooms,,use thread like groupchat or personalChat

import mongoose from 'mongoose';
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
const s3 = require("../../utils/s3");

var MsgThreadSchema = new Schema({
    users: [{
        type: ObjectId,
        ref: 'User'
    }],
    group_name: {
        type: String
    },
    created_by: {
        type: ObjectId,
        ref: 'User'
    },
    community: {
        type: ObjectId,
        ref: 'Community'
    },
    image_url: {
        type: String
    }
}, {
        timestamps: {
            createdAt: 'created_at',
            updatedAt: 'last_updated_at'
        }
    });




export default mongoose.model('MsgThread', MsgThreadSchema);
vicky
  • 415
  • 2
  • 10