The message history will be a collection of user-submitted messages through the chat application. We can choose from a number of approaches when storing such data in MongoDB. The good thing is that there is no correct implementation, although we have a number of common approaches and considerations for each implementation.
Our starting point will be that messages sent by users are part of a conversation thread. When two or more users chat with each other, initially a conversation thread is created for them. The messages become private for that conversation. This means that the messages have a parent-child relationship with another entity, a thread entity in our case.
Keeping in mind our application's requirements, we can explore the following implementations to store our messages:
Store each message in a separate document: This is the easiest to implement and it's the most flexible one, but it comes with some application-level complexity.
Embed all messages...