0

I want to store some measurement readings from devices in a mongodb time series collection.

Let's say that a device sends one or more measurement readings in each message so a message would look something like:

{ device_id: 12, timestamp: "2024-10-22T10:00:00Z", reading1: 12.4, reading2: 33.2 .... }

Is it preferable to store one reading per document or all readings in the same document or is it a "it depends" case (note that we're talking about a handful of few readings per message so hitting the 16 MB document size threshold is not a factor to consider here)?

1 Answer 1

2

One thing to consider is how you will access the data later. If you don't need to link reading1 and reading2, having two documents may make sense. If, on the other hand, both readings are used in conjunction (and it doesn't make sense to have partial data), then keep them in a single document.

Example 1: at a given moment, you get the temperature, the humidity and the barometric pressure from a sensor. Later on, you always process all three metrics together. Also imagine that if, say, the temperature is missing, the two other metics would be useless. In this case, the document should contain all three metics.

Example 2: you collect the temperature from ten different sensors once per minute. They are independent (and other probes can be added later on), but it happens that, for now at least, the application that collects the data aggregates all the measurements. This doesn't mean all ten measurements should be stored as a single document. As they are independent, store them independently. Chances are, at some point they won't even be aggregated, but collected and stored individually.


Note that for time series, specialized databases, such as InfluxDB or Prometheus, may provide more features for your specific needs. Make sure you check them and compare them (including their performance) with a general purpose database such as MongoDB.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.