i am going to design a group chat application based on mongodb, there are two schema design choices, one is designed as one document for one group chat message, another is designed as one document for all group messages.
In the first option, it can be shown as
var ChatMessageSchema = new Schema({
fromUserId: ObjectId,
toTroupeId: ObjectId,
text: String,
sent: Date
}
in the second option, it can be shown as
var ChatMessageSchema = new Schema({
toTroupeId: ObjectId,
chats:[
fromUserId: ObjectId,
text: String,
sent: Date
]
}
Both design has pros and cons, the drawback of the second option is it can hardly index on the user and search the messages from users, and also too many group message might force to create more then one documents.
The first option seems to be more reasonable since it can allow to search the message based on groupid or userid if we can index properly.
but I wonder as there are hundreds of thousands messages in the group, meaning there will be corresponding hundreds of thousands documents in one group, does this will affect the database performance?
any idea on these design choices, is the first option as the optimal one, or how to optimise it?