F
F
fedot13252016-05-11 17:57:17
MongoDB
fedot1325, 2016-05-11 17:57:17

What is the best way to make user "variables" in MongoDB?

Actually, with the transition to mongu, the question arose - how best (for the speed of obtaining this data in the future, etc.) to store user variables?
Let's say the user Vasya wanted to create variables name = Vasya, yold = 21, car = Lada Kalina. In muscle, I would store this as:
user_id | var_key | var_value
2 | name | Vasya
2 | old | 21
2 | car | Lada Kalina
Later, I can easily pull out the values ​​of the variables with the query SELECT var_value FROM variables WHERE user_id=2 AND var_key='name' ...
With mongo, I found 2 ways to store such variables.
Method 1:

{
    user_id: 2,
    variables:  [
        {
            var_key: 'name',
            var_value: 'Вася'
        },
        {
            var_key: 'yold',
            var_value: 21
        }
        ...
    ]
}

Method 2:
{
    user_id: 2,
    variables:  {
        name: 'Вася',
        yold: 21,
        ...
    }
}

Which option would be more convenient to use? Or maybe there are some other options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-05-11
@lega

1) You can add duplicate keys, you can index all values ​​​​with one index (because the key name is one), this list can be done by add / del
2) It is easier to update the value (in a direct way), gives uniqueness by key - there will be no duplicate keys by element.
Each approach has its own merits.
You can also do it the same way as in mysql, but this is often inefficient.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question