Answer the question
In order to leave comments, you need to log in
MongoDB chat best way?
My first experience with mongodb. I'm not yet sane to become a Jedi, and so I want to ask for advice.
It is necessary to make a user-user chat using Mongu. Related technologies - nodejs, socket.io, but this is not so important.
I started thinking about the document layout, and the collections themselves. For some reason Google didn't help me. Maybe I just tried badly, but it seems not.
Having estimated on a piece of paper how it might look, I painted this:
collections:
users: {
_id: login,
userId: int
name: str,
avatar: str
}
dialogs: {
precedents: [userId, userId],
messages: [{
date: ...,
text: ...,
sender: userId
}]
}
Answer the question
In order to leave comments, you need to log in
And yes, storing user logs in the incident records themselves as an array has a couple of disadvantages that may seem insignificant, but in fact they are fatal.
1. The size of the record in monge is limited - now it is 16 meg.
2. In a competitive environment, you will not be able to achieve atomicity with such a record. There will be a race.
And yes, I know what you will say - 16 meg is enough for everyone, and there will not be such an intense exchange for a race to happen. Then I will remind you about 640 kilobytes, and about Murphy's law - if any trouble can happen, it will definitely happen.
Summary - don't do it.
You made a mistake with a choice of a DB for the purposes. It is completely incomprehensible why Monga is needed here, what is the goal. The user-to-user chat works great without a database at all, I can raise a user-to-user chat on nginx without any backend at all. If you need a database just to store communication logs, any database with a buffer in the form of a fast queue (if you plan a high load) or no additional buffering at all will do.
Well, yes, _id is better for Monge to do it yourself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question