I
I
ivanesi2014-12-11 19:20:03
MongoDB
ivanesi, 2014-12-11 19:20:03

Data storage: regular object or key as value?

Good afternoon.
I use a document-oriented database (mongo).
An example object structure:

contries = [
                  {
                     name: "Russia", 
                     cities: [ 'Moscow', 'Spb' ]
                  },
                  {
                    name: "Mongolia",
                    cities: ['Ulaahn', 'Hovd']
                  }
                ]

Additional fields in the object are not expected.
But for some queries, it is more convenient for me to use this data representation:
contries = [ 
                  { "Russia" :  [ 'Moscow', 'Spb' ] },
                  { "Mongolia" : ['Ulaahn', 'Hovd'] }
                 ]

It turns out more compact, but there are vague doubts that this can be fraught with something.
Can you dispel them or share the shortcomings of the second approach, please?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Askhat Bikmetov, 2014-12-11
@askhat

A document, in the context of Mongi, is a JSON object, which in turn is a key-value set. In the first example you gave, key is name and cities, and value is a string with the name of the country and an array of cities. There are no keys in the second option, which means that it will not work db.collection.find({ country: "Russia" })and you will have to use full-text search. Decide for yourself here.
And by the way: co u ntries.

L
lega, 2014-12-11
@lega

Usually it is more convenient that the keys are fixed - no need to guess / iterate over to get the value.
The problem may arise if you add another country to each element in the future, or the like.
This will (almost) not save memory. There will be no storage issues.
If it is not planned to expand and fits well with current requests, then no problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question