|
| 1 | +Storing Time Series Data |
| 2 | +======================== |
| 3 | + |
| 4 | +.. note:: |
| 5 | + |
| 6 | + Support for mapping time series data was added in ODM 2.10. |
| 7 | + |
| 8 | +`time-series data <https://www.mongodb.com/docs/manual/core/timeseries-collections/>`__ |
| 9 | +is a sequence of data points in which insights are gained by analyzing changes |
| 10 | +over time. |
| 11 | + |
| 12 | +Time series data is generally composed of these components: |
| 13 | + |
| 14 | +- |
| 15 | + Time when the data point was recorded |
| 16 | + |
| 17 | +- |
| 18 | + Metadata, which is a label, tag, or other data that identifies a data series |
| 19 | + and rarely changes |
| 20 | + |
| 21 | +- |
| 22 | + Measurements, which are the data points tracked at increments in time. |
| 23 | + |
| 24 | +A time series document always contains a time value, and one or more measurement |
| 25 | +fields. Metadata is optional, but cannot be added to a time series collection |
| 26 | +after creating it. When using an embedded document for metadata, fields can be |
| 27 | +added to this document after creating the collection. |
| 28 | + |
| 29 | +.. note:: |
| 30 | + |
| 31 | + Support for time series collections was added in MongoDB 5.0. Attempting to |
| 32 | + use this functionality on older server versions will result in an error on |
| 33 | + schema creation. |
| 34 | + |
| 35 | +Creating The Model |
| 36 | +------------------ |
| 37 | + |
| 38 | +For this example, we'll be storing data from multiple sensors measuring |
| 39 | +temperature and humidity. Other examples for time series include stock data, |
| 40 | +price information, website visitors, or vehicle telemetry (speed, position, |
| 41 | +etc.). |
| 42 | + |
| 43 | +First, we define the model for our data: |
| 44 | + |
| 45 | +.. code-block:: php |
| 46 | +
|
| 47 | + <?php |
| 48 | +
|
| 49 | + use DateTimeImmutable; |
| 50 | + use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
| 51 | + use MongoDB\BSON\ObjectId; |
| 52 | +
|
| 53 | + #[ODM\Document] |
| 54 | + readonly class Measurement |
| 55 | + { |
| 56 | + #[ODM\Id] |
| 57 | + public string $id; |
| 58 | +
|
| 59 | + public function __construct( |
| 60 | + #[ODM\Field(type: 'date_immutable')] |
| 61 | + public DateTimeImmutable $time, |
| 62 | + #[ODM\Field(type: 'int')] |
| 63 | + public int $sensorId, |
| 64 | + #[ODM\Field(type: 'float')] |
| 65 | + public float $temperature, |
| 66 | + #[ODM\Field(type: 'float')] |
| 67 | + public float $humidity, |
| 68 | + ) { |
| 69 | + $this->id = (string) new ObjectId(); |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | +Note that we defined the entire model as readonly. While we could theoretically |
| 74 | +change values in the document, in this example we'll assume that the data will |
| 75 | +not change. |
| 76 | + |
| 77 | +Now we can mark the document as a time series document. To do so, we use the |
| 78 | +``TimeSeries`` attribute, configuring appropriate values for the time and |
| 79 | +metadata field, which in our case stores the ID of the sensor reporting the |
| 80 | +measurement: |
| 81 | + |
| 82 | +.. code-block:: php |
| 83 | +
|
| 84 | + <?php |
| 85 | +
|
| 86 | + // ... |
| 87 | +
|
| 88 | + #[ODM\Document] |
| 89 | + #[ODM\TimeSeries(timeField: 'time', metaField: 'sensorId')] |
| 90 | + readonly class Measurement |
| 91 | + { |
| 92 | + // ... |
| 93 | + } |
| 94 | +
|
| 95 | +Once we create the schema, we can store our measurements in this time series |
| 96 | +collection and let MongoDB optimize the storage for faster queries: |
| 97 | + |
| 98 | +.. code-block:: php |
| 99 | +
|
| 100 | + <?php |
| 101 | +
|
| 102 | + $measurement = new Measurement( |
| 103 | + time: new DateTimeImmutable(), |
| 104 | + sensorId: $sensorId, |
| 105 | + temperature: $temperature, |
| 106 | + humidity: $humidity, |
| 107 | + ); |
| 108 | +
|
| 109 | + $documentManager->persist($measurement); |
| 110 | + $documentManager->flush(); |
| 111 | +
|
| 112 | +Note that other functionality such as querying, using aggregation pipelines, or |
| 113 | +removing data works the same as with other collections. |
| 114 | + |
| 115 | +Considerations |
| 116 | +-------------- |
| 117 | + |
| 118 | +With the mapping above, data is stored with a granularity of seconds. Depending |
| 119 | +on how often measurements come in, we can reduce the granularity to minutes or |
| 120 | +hours. This changes how the data is stored internally by changing the bucket |
| 121 | +size. This affects storage requirements and query performance. |
| 122 | + |
| 123 | +For example, with the default ``seconds`` granularity, each bucket groups |
| 124 | +documents for one hour. If each sensor only reports data every few minutes, we'd |
| 125 | +do well to configure ``minute`` granularity. This reduces the |
| 126 | +number of buckets created, reducing storage and making queries more efficient. |
| 127 | +However, if we were to choose ``hours`` for granularity, readings for a whole |
| 128 | +month would be grouped into one bucket, resulting in slower queries as more |
| 129 | +entries have to be traversed when reading data. |
| 130 | + |
| 131 | +More details on granularity and other consideration scan be found in the |
| 132 | +`MongoDB documentation <https://www.mongodb.com/docs/manual/core/timeseries/timeseries-considerations/>`__. |
0 commit comments