|
| 1 | +.. _ruby-time-series: |
| 2 | + |
| 3 | +================ |
| 4 | +Time Series Data |
| 5 | +================ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: ruby, time series, collections, code example |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use {+driver-short+} to store |
| 24 | +and interact with **time series data**. |
| 25 | + |
| 26 | +Time series data is composed of the following components: |
| 27 | + |
| 28 | +- Measured quantity |
| 29 | +- Timestamp for the measurement |
| 30 | +- Metadata that describes the measurement |
| 31 | + |
| 32 | +The following table describes sample situations for which you could store time |
| 33 | +series data: |
| 34 | + |
| 35 | +.. list-table:: |
| 36 | + :widths: 33, 33, 33 |
| 37 | + :header-rows: 1 |
| 38 | + :stub-columns: 1 |
| 39 | + |
| 40 | + * - Situation |
| 41 | + - Measured Quantity |
| 42 | + - Metadata |
| 43 | + |
| 44 | + * - Recording monthly sales by industry |
| 45 | + - Revenue in USD |
| 46 | + - Company, country |
| 47 | + |
| 48 | + * - Tracking weather changes |
| 49 | + - Precipitation level |
| 50 | + - Location, sensor type |
| 51 | + |
| 52 | + * - Recording fluctuations in housing prices |
| 53 | + - Monthly rent price |
| 54 | + - Location, currency |
| 55 | + |
| 56 | +.. _ruby-time-series-create: |
| 57 | + |
| 58 | +Create a Time Series Collection |
| 59 | +------------------------------- |
| 60 | + |
| 61 | +.. important:: Server Version for Time Series Collections |
| 62 | + |
| 63 | + To create and interact with time series collections, you must be |
| 64 | + connected to a deployment running {+mdb-server+} 5.0 or later. |
| 65 | + |
| 66 | +To create a time series collection, you must pass an options hash that contains |
| 67 | +the specifications for the collection. You can specify the following specifications |
| 68 | +for your time series collection: |
| 69 | + |
| 70 | +- ``:timeField``: Specifies the field that stores a timestamp in each time series |
| 71 | + document. |
| 72 | +- ``:metaField``: Specifies the field that stores metadata in each time series |
| 73 | + document. |
| 74 | +- ``:granularity``: Specifies the approximate time between consecutive timestamps. |
| 75 | + The possible values are ``'seconds'``, ``'minutes'``, and ``'hours'``. |
| 76 | +- ``:bucketMaxSpanSeconds``: Sets the maximum time between timestamps in the |
| 77 | + same bucket. |
| 78 | +- ``:bucketRoundingSeconds``: Sets the number of seconds to round down by when |
| 79 | + MongoDB sets the minimum timestamp for a new bucket. Must be equal to |
| 80 | + ``:bucketMaxSpanSeconds``. |
| 81 | + |
| 82 | +See :manual:`Command Fields </reference/command/create/#command-fields>` |
| 83 | +in the {+mdb-server+} manual entry on the ``create`` command to learn more about |
| 84 | +these parameters. |
| 85 | + |
| 86 | +Example |
| 87 | +~~~~~~~ |
| 88 | + |
| 89 | +The following example uses the ``Collection#create`` method to create a time series |
| 90 | +collection named ``october2024`` with the ``:timeField``` option set to ``"timestamp"``: |
| 91 | + |
| 92 | +.. literalinclude:: /includes/usage-examples/time-series.rb |
| 93 | + :language: ruby |
| 94 | + :dedent: |
| 95 | + :start-after: start-create |
| 96 | + :end-before: end-create |
| 97 | + |
| 98 | +To verify that you have successfully created the collection, print a list of all |
| 99 | +collections in your database and filter by collection name, as shown in the following |
| 100 | +code: |
| 101 | + |
| 102 | +.. io-code-block:: |
| 103 | + |
| 104 | + .. input:: /includes/usage-examples/time-series.rb |
| 105 | + :language: ruby |
| 106 | + :start-after: start-correct |
| 107 | + :end-before: end-correct |
| 108 | + :dedent: |
| 109 | + |
| 110 | + .. output:: |
| 111 | + :language: json |
| 112 | + :visible: false |
| 113 | + |
| 114 | + [ |
| 115 | + { |
| 116 | + "name": "october2024", |
| 117 | + "type": "timeseries", |
| 118 | + "options": { |
| 119 | + "timeseries": { |
| 120 | + "timeField": "timestamp", |
| 121 | + "granularity": "seconds", |
| 122 | + "bucketMaxSpanSeconds": 3600 |
| 123 | + } |
| 124 | + }, |
| 125 | + "info": { |
| 126 | + "readOnly": false |
| 127 | + } |
| 128 | + } |
| 129 | + ] |
| 130 | + |
| 131 | + |
| 132 | +.. _ruby-time-series-write: |
| 133 | + |
| 134 | +Store Time Series Data |
| 135 | +---------------------- |
| 136 | + |
| 137 | +You can insert data into a time series collection by using the ``insert_one`` |
| 138 | +or ``insert_many`` method and specifying the measurement, timestamp, and |
| 139 | +metadata in each inserted document. |
| 140 | + |
| 141 | +To learn more about inserting documents, see the :ref:`ruby-write-insert` guide. |
| 142 | + |
| 143 | +Example |
| 144 | +~~~~~~~ |
| 145 | + |
| 146 | +This example inserts New York City temperature data into the ``october2024`` |
| 147 | +time series collection created in the preceding :ref:`ruby-time-series-create` |
| 148 | +section. Each document contains the following fields: |
| 149 | + |
| 150 | +- ``temperature``, which stores temperature measurements in degrees Fahrenheit |
| 151 | +- ``location``, which stores location metadata |
| 152 | +- ``timestamp``, which stores the measurement timestamp |
| 153 | + |
| 154 | +.. literalinclude:: /includes/usage-examples/time-series.rb |
| 155 | + :language: ruby |
| 156 | + :dedent: |
| 157 | + :start-after: start-insert |
| 158 | + :end-before: end-insert |
| 159 | + |
| 160 | +.. TODO: add link |
| 161 | + |
| 162 | +.. .. tip:: Formatting Dates and Times |
| 163 | + |
| 164 | + .. To learn more about using ``datetime`` objects in {+driver-short+}, see |
| 165 | + .. :ref:`ruby-dates-times`. |
| 166 | + |
| 167 | +.. _ruby-time-series-read: |
| 168 | + |
| 169 | +Query Time Series Data |
| 170 | +---------------------- |
| 171 | + |
| 172 | +You can use the same syntax and conventions to query data stored in a time |
| 173 | +series collection as you use when performing read or aggregation operations on |
| 174 | +other collections. |
| 175 | + |
| 176 | +.. TODO: add links |
| 177 | +.. To learn more about these operations, see :ref:`ruby-read` |
| 178 | +.. and :ref:`ruby-aggregation`. |
| 179 | + |
| 180 | +.. _ruby-time-series-addtl-info: |
| 181 | + |
| 182 | +Additional Information |
| 183 | +---------------------- |
| 184 | + |
| 185 | +To learn more about the concepts in this guide, see the following {+mdb-server+} |
| 186 | +manual entries: |
| 187 | + |
| 188 | +- :manual:`Time Series </core/timeseries-collections/>` |
| 189 | +- :manual:`Create and Query a Time Series Collection </core/timeseries/timeseries-procedures/>` |
| 190 | +- :manual:`Set Granularity for Time Series Data </core/timeseries/timeseries-granularity/>` |
| 191 | + |
| 192 | +API Documentation |
| 193 | +~~~~~~~~~~~~~~~~~ |
| 194 | + |
| 195 | +To learn more about the methods mentioned in this guide, see the following |
| 196 | +API documentation: |
| 197 | + |
| 198 | +- `create <{+api-root+}/Mongo/Collection.html#create-instance_method>`__ |
| 199 | +- `list_collections <{+api-root+}/Mongo/Database.html#list_collections-instance_method>`__ |
| 200 | +- `insert_one <{+api-root+}/Mongo/Collection.html#insert_one-instance_method>`__ |
| 201 | +- `insert_many <{+api-root+}/Mongo/Collection.html#insert_many-instance_method>`__ |
| 202 | + |
0 commit comments