I have an app with chatrooms. Each chatroom has its own collection in mongodb. Along with other info, I want to save the chat history for each chat chatroom, in a "messages" field.
Database: myApp
Collection: "chatroom_1"
For example, the collection "chatroom_coders" should have:
{
roomName: coders,
messages: {
0: {sentByUserID: 1, message: 'hi', chatID: 0},
1: {sentByUserID: 1, message: 'hi', chatID: 1},
100: {sentByUserID: 1, message: 'hi', chatID: 100},
},
filesShared: ['penguin.png', 'turtle.png'],
created_at: null,
updated_at: null
}
Then I want to retrieve the data as it is stored above. Then I will be manipulating it (on my side, in JS) for example delete chatrooms['chatroom_coders'].messages[0]
, and will need to update the collection to remove that field.
Not sure yet how to achieve what I want and couldn't find the right info yet on the web. How do I add my message to the messages (and create that messages object, as well) to that specific chatroom collection? Suggestions on improving the way I want to store data in mongodb would be appreciated too.