Reputation: 298
I made a chat system, and all the information in the chatroom is expected to be stored through MongoDB's Document. Each message sent is appended to the room messages array.
The data structure is as follows:
This blog thinks this is the right storage.
But the official documentation says each Document cannot be more than 16 MB
Upvotes: 0
Views: 291
Reputation: 298
As a follow-up to my own question:
I need to control the length of room messages to be less than 100, similar to the queue, first-in, first-out method.
The sample code for golang is as follows:
query := bson.M{
"room_customer.customer_id": msg.FromUserName,
}
changes := bson.M{
"$push": bson.M{"room_messages": bson.M{"$each": []model.RoomMessage{
{
Msg: msgText,
CreateTime: time.Now(),
},
},
"$slice": -100}},
}
roomCollection.Update(query, changes)
In this way, you can keep the chat record of the conversation room up to 100 messages at all times, I hope it will be helpful for people who encounter similar problems!
Upvotes: 0
Reputation: 430
It really depends on how you will query the data, but I think the design is OK. Having all the messages inside a chat document will guarantee you to have consistency and keep your chats safe regarding concurrency issues.
About the limit of 16MB, I think it is quite OK.
Anyway, I wouldn't keep the whole history of messages inside the document for performance reasons (it can become very slow to query if you have very busy chatrooms) but I would limit the size of the list of chats to, let's say 100 elements (the most recent ones) and I would use another approach to backup the whole chatroom history.
Upvotes: 1