Take facebook\'s private messaging system where you have to keep track of sender and receiver along w/ the message content. If I were using MySQL I would have multiple table
Your Db Schema Should be like this->
let chatImages = {
original : {type:String,required:true,trim:true},
thumbnail : {type:String,required:true,trim:true}
};
let Chats = new Schema({
CommonId : {type: String, trim: true, index: true, unique: true,sparse: true},
senderId : {type: Schema.Types.ObjectId,index: true,required:true,ref:'User'},
receiverId : {type: Schema.Types.ObjectId,index: true,required:true,ref:'User'},
messageId : {type: String, trim: true, index: true, unique: true,sparse: true},
isDeliver : {type: Boolean, default: false},
isSent : {type: Boolean, default: true},
chatType : {
type: String,required:true,enum: [
Config.APP_CONSTANTS.DATABASE.CHAT_TYPE.TEXT,
Config.APP_CONSTANTS.DATABASE.CHAT_TYPE.IMAGE
],default:Config.APP_CONSTANTS.DATABASE.CHAT_TYPE.TEXT},
text : {type: String, trim: true, index: true,sparse: true},
sentAt : {type:Number, default: Date.now,index:true,required: true},
chatImage : {type:chatImages},
});
CommonId is very Important if you want the screen like whatsApp where you can see the persons with whom you chat. 1)It Make Grouping very easy(with $groupBy).
You can Generate CommonId by just Compare between receiverId and SenderId which is ascending put it CommonId
I think its a nice Explaination