In my code, I have a Chats model which stored information about a Chat. It includes the users in the chat as well as other information such as createdAt and updatedAt.
I also have a Message model which has properties relating to the message such as sentBy as well as messageText.
Users can star/save messages and then view all of the starred messages that they have for a particular Chat
The problem I have is knowing where to store this information. In the Chat or Messages model.
If stored in the Chat model then the structure would look like so.
ChatObj: {
users: [UserIds],
createdAt: Date,
updatedAt: Date,
chatTitle: String,
starredMessages: {
UserID1: [
StarredMessageId1,
StarredMessageId2
],
UserID2: [
StarredMessageId1,
StarredMessageId2
],
}
}
starredMessageswould contain the UserId as a key, and an array of all starred message id's as a value.
An alternative is to store starredMessages in the Messages model. This would look like this:
MessageObj: {
sentBy: [UserId],
text: String,
starredBy: [userIds]
}
The message object would simply contain an array of user id's whom have starred that particular message
When retrieving all messages that a user has starred for a particular chat, it would be much more efficient using option 1, as you would simply look up the key which is the user id in order to get all the results. With option 2, you would have to search through all the message objects (could be dozens), to see if the user id is present in the starredBy array.
Is it correct to assume method 1 would be more appropriate due to the reason described above (more efficient), or would option 2 make more sense in terms of where to store this information regarding starred messages Thanks.