Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB historical data storage - best practice?

Assuming I have an user who has an ID and I want to store a historical record (document) on this user every day, what is better:

  • create a new document for each record and search for the user id; or
  • keep updating and embedding that data into one single user document which keeps growing over time?

Mostly I want to retrieve only the current document for the user but all records should be accessible at any time without a super long search/query.

like image 657
Christian Smorra Avatar asked Jan 25 '12 16:01

Christian Smorra


2 Answers

There are a lot of variables that can affect such a decision. One big document seems most obvious provided it doesn't grow to unpractically large or even disallowed sizes (mind you, a document can be at most 16MB in size).

Using document per entry is also perfectly viable and provided you create the appropriate indexes should not result in slow queries.

like image 192
Remon van Vliet Avatar answered Nov 08 '22 14:11

Remon van Vliet


There is a limit to how big a document can be. It's (as of v1.8) 16 MB. So you can simply run out of room if you update & embed. Also, mongo allocates document space based on average document size in a collection. If you keep adjusting/resizing this might have negative performance implications.

I think it's much safer to create new documents for each record and if/when you want to collate that data, you do it in a map/reduce job.

like image 27
Mark Bolusmjak Avatar answered Nov 08 '22 14:11

Mark Bolusmjak