Hi, in my application I’m developing I need to allow for chats between any 2 users. I have the following schema in mind for a Messages collection:
{
_id: ObjectId(),
userID1: "userA_ID",
userID2: "userB_ID",
sender: "one of the users",
message: "some text",
timestamp: time
}
userID1
will always be the first user to start the chat, I’ll save this for every Chat room consisting of 2 people. (EDIT: Should I use lexicographical ordering instead, with userID1 coming before userID2?)
I’ll create an index on the timestamp field so I can sort it in reverse, and then I can do the following to get the data every time the chat room is loaded (with pagination as you scroll up):
db.find({ userID1, userID2 }).sort({ "timestamp" : -1 })
.skip(offset).limit(limit)
And this I think should give me my intended behavior.
- Does this make sense? Is there anything I’m missing out or have overlooked?
- Is this the correct practice, storing ALL user messages in one big Messages collection? Of course I’ll be implementing all necessary security/privacy protection but the fact still remains that all these messages are stored in one large collection. Is this a concern from a security/privacy/logical standpoint?
Appreciate any advice, thanks.