MongoDB historical data memory - best practice?

Assuming I have a user that has an identifier, and every day I want to keep a historical record (document), which is better:

  • create a new document for each entry and search for user ID; or
  • continue to update and embed this data in one user document, which is growing over time?

Basically I want to get only the current document for the user, but all entries should be available at any time without a super long search / query.

+6
source share
2 answers

There are many variables that can influence such a decision. One large document seems most obvious if it does not grow to impractically large or even forbidden sizes (of course, the size of the document can be no more than 16 MB).

Using a document for each record is also quite viable and should not lead to slow queries when creating the appropriate indexes.

+3
source

There is a limit on the size of the document. It (version 1.8) is 16 MB. This way you can just leave the room if you update and insert. In addition, mongo allocates document space based on the average size of the document in the collection. If you continue to adjust / resize, this can have a negative performance impact.

I think it is much safer to create new documents for each record, and if / when you want to match this data, you do it on the map / job reduction.

+4
source

Source: https://habr.com/ru/post/906882/


All Articles