schedar
schedar

Reputation: 429

MongoDB schema design for a measurement acquisition system

Introduction:

Problem:

My main concern is about the documents that grow a lot over time (inserting to embedded array of measurements) and the general document structure that makes the measurements hard to query for a given date/time range.

E.g. Even if there was only one node reporting data each 5 seconds, then the total number of measurements in embedded array (only for one day) is: 24*60*60/5=17280. Having 5 nodes reporting for a month gives: 5 embedded arrays with 518400 elements (in one document!). The longer the device works, the more entries it has in embedded array of measurements for each node attached.

Questions:

Upvotes: 2

Views: 333

Answers (1)

Remon van Vliet
Remon van Vliet

Reputation: 18625

In order :

  • It doesn't. Embedding an infinitely growing structure in a single document does not scale and should be avoided. It is preferable by far to store each measurement as a single document. The read/write ratio is not very relevant once you go for that although write performance will be more stable (MongoDB has to move growing documents regularly which can cause write latency spikes).
  • There are actually not a lot of "good things" about embedding. It complicates querying, there's no way to get a small part of the embedded structure and so forth. As such it is not only justified but highly encouraged to move to two seperate collections. In future proof schemas you embed if, and only if, you always need the entire embedded structure if you query the top level document and if that embedded structure is size bound regardless of how many users or data your system has to deal with.

Upvotes: 2

Related Questions