Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a good idea to store chat messages in a mongodb collection?

I'm developing a chat app with node.js, redis, socket.io and mongodb. MongoDB comes the last and for persisting the messages.

My question is what would be the best approach for this last step?

I'm afraid a collection with all the messages like

{
    id,
    from,
    to,
    datetime,
    message
}

can get too big too soon, and is going to get very slow for reading purposes, what do you think?

Is there a better approach you already worked with?

like image 657
R01010010 Avatar asked Aug 04 '16 11:08

R01010010


People also ask

Is MongoDB good for chat application?

MongoDB is used for many chat applications. The primary benefits associated with the document model enable you to seamlessly extend your application as you build functionality.

Which database is good for storing chat messages?

This underpinning of development is carried in MirrorFly infrastructure which offers the best database design for storing chat messages for diminishing the queuing of messages and consumption of data.

What should I store in MongoDB?

Documents are used to store data in MongoDB. These documents are saved in JSON (JavaScript Object Notation) format in MongoDB. JSON documents support embedded fields, allowing related data and data lists to be stored within the document rather than in an external table. JSON is written in the form of name/value pairs.


1 Answers

In MongoDB, you store your data in the format you will want to read them later.

If what you read from the database is a list of messages filtered on the 'to' field and with a dynamic datetime filter, then this schema is the perfect fit.

Don't forget to add an index on the fields you will be querying on, then it will be reasonable fast to query them, even over millions of records.

If you would, for example, always show a full history of a full day, you would store all messages for a single day in one document. If both types of queries occur a lot, you would even store your messages in both formats.

If storage is an issue, you could also use capped collection, which will automatically delete messages of e.g. over 1 year old.

like image 190
GeertPt Avatar answered Oct 21 '22 05:10

GeertPt