D
D
Damir Makhmutov2013-12-09 16:35:30
MongoDB
Damir Makhmutov, 2013-12-09 16:35:30

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
}

userId - identifier in the main database (mysql) of the user. For some reason, my gut told me to use login as _id, and it is unique. Maybe I'm wrong?
Further:
dialogs: {
    precedents: [userId, userId],
    messages: [{
       date: ...,
       text: ...,
       sender: userId
    }]
}

And then the question arose: is it correct to store messages in an array field of the document? How is the performance with this scheme?
In general, I would like to hear advice on how best to organize the storage of such data in mongo.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2013-12-09
@doodoo

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.

A
Andrew, 2013-12-09
@kaasius

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 question

Ask a Question

731 491 924 answers to any question