Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Chat Schema

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);
like image 559
Andrés Da Viá Avatar asked Feb 12 '15 16:02

Andrés Da Viá


People also ask

Is MongoDB good for chat application?

MongoDB is used for many chat applications. The primary benefits associated with the document model enable you to seamlessly extend your application as you build functionality.

Is there a schema in MongoDB?

Data in MongoDB has a flexible schema. Collections do not enforce document structure by default. This flexibility gives you data-modeling choices to match your application and its performance requirements. An introduction to data modeling in MongoDB.

What is a MongoDB schema?

What is a Schema? A schema is a JSON object that defines the the structure and contents of your data. You can use Atlas App Services' BSON schemas, which extend the JSON Schema standard, to define your application's data model and validate documents whenever they're created, changed, or deleted.


1 Answers

//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);
like image 90
vicky Avatar answered Sep 20 '22 12:09

vicky