I have created Group Chat Schema like this
User : {
group_id:String,
name: String,
user_id:String,
last_seen:String
}
Message : {
message:String,
user_id:String,
group_id:String,
created_at:String
}
Group : {
group_id:String,
total_messages:String
}
Need to perform few queries when the individual user gets his/her group
- All groups where the user is available with unread count sorted in latest order
- Paginated list of chat
What I have done so far
-
User sends his/her id, matches current id in the USER collection which gives all groups of a specified user, and then gets unread messages from another MESSAGE collection using last seen date. I don’t want to perform any lookup or group operation when designing this query. As both operations incur overhead. So is there any way to redesign this schema?
-
I am maintaining a separate collection for total messages in the group so that I don’t want to perform any count operation in real-time. And for getting chat I have applied index on a date so on query it gives me latest messages varied according to limit and offset and later I applied sort method of javascript. So is there any other way better than this. Can I get formatted array for db only ?