|
| 1 | +.. _kotlin-sync-time-series: |
| 2 | + |
| 3 | +================ |
| 4 | +Time Series Data |
| 5 | +================ |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to use the {+driver-short+} to store |
| 17 | +and interact with **time series data**. |
| 18 | + |
| 19 | +Time series data is composed of the following components: |
| 20 | + |
| 21 | +- Measured quantity |
| 22 | +- Timestamp for the measurement |
| 23 | +- Metadata that describes the measurement |
| 24 | + |
| 25 | +The following table describes sample situations for which you could store time |
| 26 | +series data: |
| 27 | + |
| 28 | +.. list-table:: |
| 29 | + :widths: 33, 33, 33 |
| 30 | + :header-rows: 1 |
| 31 | + :stub-columns: 1 |
| 32 | + |
| 33 | + * - Situation |
| 34 | + - Measured Quantity |
| 35 | + - Metadata |
| 36 | + |
| 37 | + * - Recording monthly sales by industry |
| 38 | + - Revenue in USD |
| 39 | + - Company, country |
| 40 | + |
| 41 | + * - Tracking weather changes |
| 42 | + - Precipitation level |
| 43 | + - Location, sensor type |
| 44 | + |
| 45 | + * - Recording fluctuations in housing prices |
| 46 | + - Monthly rent price |
| 47 | + - Location, currency |
| 48 | + |
| 49 | +.. _cpp-time-series-create: |
| 50 | + |
| 51 | +Create a Time Series Collection |
| 52 | +------------------------------- |
| 53 | + |
| 54 | +.. important:: Server Version for Time Series Collections |
| 55 | + |
| 56 | + To create and interact with time series collections, you must be |
| 57 | + connected to a deployment running {+mdb-server+} 5.0 or later. |
| 58 | + |
| 59 | +You can create a time series collection to store time series data. |
| 60 | +To create a time series collection, pass the following parameters to the |
| 61 | +``createCollection()`` method: |
| 62 | + |
| 63 | +- The name of the new collection to create |
| 64 | + |
| 65 | +- A `CreateCollectionOptions <{+core-api+}/com/mongodb/client/model/CreateCollectionOptions.html>`__ |
| 66 | + object with the `TimeSeriesOptions <{+core-api+}/com/mongodb/client/model/TimeSeriesOptions.html>`__ set |
| 67 | + using the ``timeSeriesOptions()`` method |
| 68 | + |
| 69 | +.. _kotlin-sync-time-series-create-example: |
| 70 | + |
| 71 | +Example |
| 72 | +~~~~~~~ |
| 73 | + |
| 74 | +This example creates the ``october2024`` time series collection in the |
| 75 | +``fall_weather`` database with the ``timeField`` option set to the ``"timestamp"`` field: |
| 76 | + |
| 77 | +.. literalinclude:: /includes/data-formats/time-series.kt |
| 78 | + :language: kotlin |
| 79 | + :start-after: start-create-time-series |
| 80 | + :end-before: end-create-time-series |
| 81 | + :dedent: |
| 82 | + |
| 83 | +To verify that you successfully created the time series collection, run |
| 84 | +the ``listCollections()`` method on the database and print the results: |
| 85 | + |
| 86 | +.. io-code-block:: |
| 87 | + :copyable: true |
| 88 | + |
| 89 | + .. input:: /includes/data-formats/time-series.kt |
| 90 | + :language: kotlin |
| 91 | + :start-after: start-print-time-series |
| 92 | + :end-before: end-print-time-series |
| 93 | + :dedent: |
| 94 | + |
| 95 | + .. output:: |
| 96 | + |
| 97 | + { |
| 98 | + "name": "october2024", |
| 99 | + "type": "timeseries", |
| 100 | + "options": { |
| 101 | + "timeseries": { |
| 102 | + "timeField": "temperature", |
| 103 | + "granularity": "seconds", |
| 104 | + "bucketMaxSpanSeconds": 3600 |
| 105 | + } |
| 106 | + }, |
| 107 | + "info": { |
| 108 | + "readOnly": false |
| 109 | + } |
| 110 | + } |
| 111 | + ... |
| 112 | + |
| 113 | +.. _kotlin-sync-time-series-store: |
| 114 | + |
| 115 | +Store Time Series Data |
| 116 | +---------------------- |
| 117 | + |
| 118 | +You can insert data into a time series collection by using the ``insertOne()`` |
| 119 | +or ``insertMany()`` methods and specifying the measurement, timestamp, and metadata |
| 120 | +in each inserted document. |
| 121 | + |
| 122 | +.. TODO: Uncomment when insert guide is created |
| 123 | +.. .. tip:: |
| 124 | + |
| 125 | +.. To learn more about inserting documents into a collection, see the :ref:`kotlin-sync-write-insert` |
| 126 | +.. guide. |
| 127 | + |
| 128 | +Example |
| 129 | +~~~~~~~ |
| 130 | + |
| 131 | +This example inserts New York City temperature data into the ``october2024`` |
| 132 | +time series collection created in the :ref:`Create a Time Series Collection example |
| 133 | +<kotlin-sync-time-series-create-example>`. Each document contains the following fields: |
| 134 | + |
| 135 | +- ``temperature``, which stores temperature measurements in degrees Fahrenheit |
| 136 | +- ``location``, which stores location metadata |
| 137 | +- ``timestamp``, which stores the time of the measurement collection |
| 138 | + |
| 139 | +.. literalinclude:: /includes/data-formats/time-series.kt |
| 140 | + :language: kotlin |
| 141 | + :start-after: start-insert-time-series-data |
| 142 | + :end-before: end-insert-time-series-data |
| 143 | + :dedent: |
| 144 | + |
| 145 | +.. _kotlin-sync-time-series-query: |
| 146 | + |
| 147 | +Query Time Series Data |
| 148 | +---------------------- |
| 149 | + |
| 150 | +You can use the same syntax and conventions to query data stored in a time |
| 151 | +series collection as you use when performing read or aggregation operations on |
| 152 | +other collections. To learn more about these operations, see |
| 153 | +the :ref:`Additional Information <kotlin-sync-time-series-addtl-info>` section. |
| 154 | + |
| 155 | +.. _kotlin-sync-time-series-addtl-info: |
| 156 | + |
| 157 | +Additional Information |
| 158 | +---------------------- |
| 159 | + |
| 160 | +To learn more about the concepts mentioned in this guide, see the |
| 161 | +following {+mdb-server+} manual entries: |
| 162 | + |
| 163 | +- :manual:`Time Series </core/timeseries-collections/>` |
| 164 | +- :manual:`Create and Query a Time Series Collection </core/timeseries/timeseries-procedures/>` |
| 165 | +- :manual:`Set Granularity for Time Series Data </core/timeseries/timeseries-granularity/>` |
| 166 | + |
| 167 | +To learn more about performing read operations, see :ref:`kotlin-sync-read`. |
| 168 | + |
| 169 | +.. TODO: To learn more about performing aggregation operations, see the :ref:`kotlin-sync-aggregation` |
| 170 | +.. guide. |
| 171 | + |
| 172 | +API Documentation |
| 173 | +~~~~~~~~~~~~~~~~~ |
| 174 | + |
| 175 | +To learn more about the methods mentioned in this guide, see the following |
| 176 | +API documentation: |
| 177 | + |
| 178 | +- `createCollection() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-database/create-collection.html>`__ |
| 179 | +- `listCollections() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-database/list-collections.html>`__ |
| 180 | +- `insertOne() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/insert-one.html>`__ |
| 181 | +- `insertMany() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/insert-many.html>`__ |
0 commit comments