Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing mongodb for a chat application?

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.

like image 791
Forivin Avatar asked Sep 15 '25 01:09

Forivin


2 Answers

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/

like image 109
Dmitrijs Balcers Avatar answered Sep 16 '25 20:09

Dmitrijs Balcers


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.

like image 33
webdeb Avatar answered Sep 16 '25 18:09

webdeb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!