I've only worked with mySQL in the past and I'm really unsure on how to design the tables in mongodb.
I'm basically trying to write the backend for a little chat application.
Is there something like a best practice for the structure for a case like this? How would you advice me to do it?
At the moment I'm thinking about something like this:
users
{
id,
name,
status,
lastActivity,
chartRoomIds
}
chatRooms
{
id,
name
}
messages
{
id,
timeSent,
content,
userId,
chatRoomId
}
But that would mean you'd have to go through all the messages to find only the ones for a specific chat room. So maybe it would be better to store the messages directly in the chat room object?
I'd really like to see how the experienced ones of you would structure this example.
I think, that you simply have to read the MongoDB documentation.
Here are described basic mongoDB data modelling patterns: http://docs.mongodb.org/manual/data-modeling/
you for an chat app, where you need some real time capabilities you could try/prototype something with meteor. Meteor uses mongo by default and gives you some really good tutorials about Mongo with meteor and so on.
To answer your question:
you'd have to go through all the messages
Right, but if you use proper indexing it will also be no problem. If you store the messages in the chatRooms, you would need to aggregate the massages from it and so on..
Your structure is ok, you can create an index with
db.messages.ensureIndex('roomId', 1)
The index is stored in the memory, so the it will filter the messages by roomId in no-time. Try http://robomongo.org/ which let you query your db like PHPMyAdmin or Sequel Pro for MySQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With