Reputation: 12204
I'm building a chat server app using node.js.
I need to save chat transcripts on the database and would like to play with nosql database like mongodb. If i was in relational db world, i would create users, chat_sessions and chat_messages tables and, for every new message, i'd append new record in chat_messages table.
Which is the best approach in nosql?
Do i have to create a chat_session document and, inside of it, create a chat_messages structure that is updated for every new message or is there a better way to do it for nosql/mongodb?
Upvotes: 1
Views: 1416
Reputation: 30136
You would use a similar approach and insert each new message as a separate document into the collection (possibly one for each room or channel).
Documents in MongoDB have a 16mb limit so storing the entire history in one document, which grows in an unbounded fashion, would be a bad choice.
You can de-normalize usernames and store them on the messages themselves to avoid querying two tables (this would likely be a join in a relational database).
It may make sense to split the data into multiple databases rather than collections (per room, channel, whatever) because currently mongodb has a database level write lock. This would allow you to achieve greater write concurrency with mongodb and node.js.
Upvotes: 2