choose the mongoose schema on group chat application? choose the mongoose schema on group chat application? mongodb mongodb

choose the mongoose schema on group chat application?


I would suggest a third option; creating a new collection for every group, e.g. room_$groupid. In such a collection, you could insert every message separately. This would give you the benefit of getting a full chatroom without a filter. You could simply return the last 200 or so messages from the collection.

It would allow for easier scalability, cause you won't end up with a single massive collection that you would have to filter through.

However, you would have to write the logic for selecting the right collection but should be a fairly trivial task.The downside would be that it would be near impossible to do a text search over multiple groups without throwing performance out of the window.

Collection limit*


MongoDB is made to handle huge amounts of data and their PDF Performance Best Practices for MongoDB states:

Avoir large documents

Which is also made clear by the 16MB limit.

So one can argue that MongoDB is specifically designed to handle hundreds of thousand of documents corresponding to your first schema.

Simply reduce the number of indexes to what you need (do you really need to query by users that often or could accept that query to be a lot slower ?) and you should be fine with your first schema. Actually I'm not sure there is any benefit with the second one.