N
N
nexcode2016-02-25 20:16:26
MongoDB
nexcode, 2016-02-25 20:16:26

How to properly organize the storage of the same type of information in MongoDB?

For example, there are such data structures:

{
  key: string,
  value1: string,
  value2: string,
  value3: string,
  value4: string
}

There are two ways to store this in MongoDB.
Method one (inserting subdocuments into the document):
{
  _id: xxx,
  key1: {
    value1: string,
    value2: string,
    value3: string,
    value4: string
  },
  key2: {
    value1: string,
    value2: string,
    value3: string,
    value4: string
  },
  key3: {
    value1: string,
    value2: string,
    value3: string,
    value4: string
  },
  key4: {
    value1: string,
    value2: string,
    value3: string,
    value4: string
  }
}

Method two (use an array):
{
  _id: xxx,
  array: [{
    key: string,
    value1: string,
    value2: string,
    value3: string,
    value4: string
  }, {
    key: string,
    value1: string,
    value2: string,
    value3: string,
    value4: string
  }, {
    key: string,
    value1: string,
    value2: string,
    value3: string,
    value4: string
  }, {
    key: string,
    value1: string,
    value2: string,
    value3: string,
    value4: string
  }]
}

Which of the methods is more rational, provided that the operations of updating field values ​​and inserting new subdocuments predominate.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-02-25
@nexcode

If you need direct access by key, for example, update by key, then the 1st one is more
convenient
. rather the 2nd one would be better, but still depends on the data and queries.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question