|
| 1 | +.. _java-rs-time-series: |
| 2 | + |
| 3 | +================ |
| 4 | +Time Series Data |
| 5 | +================ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, measurement, weather |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 1 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+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 | +.. _java-rs-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 | +You can create a time series collection to store time series data. |
| 67 | +To create a time series collection, pass the following parameters to the |
| 68 | +``createCollection()`` method: |
| 69 | + |
| 70 | +- The name of the new collection to create |
| 71 | + |
| 72 | +- A `CreateCollectionOptions <{+api+}/mongodb-driver-core/com/mongodb/client/model/CreateCollectionOptions.html>`__ |
| 73 | + object with the `TimeSeriesOptions <{+api+}/mongodb-driver-core/com/mongodb/client/model/TimeSeriesOptions.html>`__ set |
| 74 | + with the ``timeSeriesOptions()`` method |
| 75 | + |
| 76 | +.. _java-rs-time-series-create-example: |
| 77 | + |
| 78 | +The following example creates a time series collection named ``october2024`` in the |
| 79 | +``fall_weather`` database with the ``timeField`` option set to the ``"timestamp"`` field: |
| 80 | + |
| 81 | +.. literalinclude:: /includes/data-formats/time-series.java |
| 82 | + :language: java |
| 83 | + :start-after: start-create-time-series |
| 84 | + :end-before: end-create-time-series |
| 85 | + :dedent: |
| 86 | + |
| 87 | +To verify that you successfully created the time series collection, run |
| 88 | +the ``listCollections()`` method on the database and print the results: |
| 89 | + |
| 90 | +.. io-code-block:: |
| 91 | + :copyable: true |
| 92 | + |
| 93 | + .. input:: /includes/data-formats/time-series.java |
| 94 | + :language: java |
| 95 | + :start-after: start-print-time-series |
| 96 | + :end-before: end-print-time-series |
| 97 | + :dedent: |
| 98 | + |
| 99 | + .. output:: |
| 100 | + |
| 101 | + Document{{name=october2024, type=timeseries, options=Document{{timeseries=Document{{timeField=timestamp, granularity=seconds, bucketMaxSpanSeconds=3600}}}}, info=Document{{readOnly=false}}}} |
| 102 | + ... |
| 103 | + |
| 104 | +.. _java-rs-time-series-store: |
| 105 | + |
| 106 | +Store Time Series Data |
| 107 | +---------------------- |
| 108 | + |
| 109 | +You can insert data into a time series collection by using the ``insertOne()`` |
| 110 | +or ``insertMany()`` methods and specifying the measurement, timestamp, and metadata |
| 111 | +in each inserted document. |
| 112 | + |
| 113 | +.. tip:: |
| 114 | + |
| 115 | + To learn more about inserting documents into a collection, see the :ref:`java-rs-write-insert` |
| 116 | + guide. |
| 117 | + |
| 118 | +Example |
| 119 | +~~~~~~~ |
| 120 | + |
| 121 | +The following example inserts New York City temperature data into the ``october2024`` |
| 122 | +time series collection created in the :ref:`Create a Time Series Collection example |
| 123 | +<java-rs-time-series-create-example>`. Each document contains the following fields: |
| 124 | + |
| 125 | +- ``temperature``, which stores temperature measurements in degrees Fahrenheit |
| 126 | +- ``location``, which stores location metadata |
| 127 | +- ``timestamp``, which stores the time of the measurement collection |
| 128 | + |
| 129 | +.. literalinclude:: /includes/data-formats/time-series.java |
| 130 | + :language: java |
| 131 | + :start-after: start-insert-time-series-data |
| 132 | + :end-before: end-insert-time-series-data |
| 133 | + :dedent: |
| 134 | + |
| 135 | +.. _java-rs-time-series-query: |
| 136 | + |
| 137 | +Query Time Series Data |
| 138 | +---------------------- |
| 139 | + |
| 140 | +You can use the same syntax and conventions to query data stored in a time |
| 141 | +series collection as you use when performing read or aggregation operations on |
| 142 | +other collections. To learn more about these operations, see |
| 143 | +the :ref:`Additional Information <java-rs-time-series-addtl-info>` section. |
| 144 | + |
| 145 | +.. _java-rs-time-series-addtl-info: |
| 146 | + |
| 147 | +Additional Information |
| 148 | +---------------------- |
| 149 | + |
| 150 | +To learn more about the concepts mentioned in this guide, see the |
| 151 | +following {+mdb-server+} manual entries: |
| 152 | + |
| 153 | +- :manual:`Time Series </core/timeseries-collections/>` |
| 154 | +- :manual:`Create and Query a Time Series Collection </core/timeseries/timeseries-procedures/>` |
| 155 | +- :manual:`Set Granularity for Time Series Data </core/timeseries/timeseries-granularity/>` |
| 156 | + |
| 157 | +To learn more about performing read operations, see :ref:`java-rs-read`. |
| 158 | + |
| 159 | +To learn more about performing aggregation operations, see the :ref:`java-rs-aggregation` |
| 160 | +guide. |
| 161 | + |
| 162 | +API Documentation |
| 163 | +~~~~~~~~~~~~~~~~~ |
| 164 | + |
| 165 | +To learn more about the methods mentioned in this guide, see the following |
| 166 | +API documentation: |
| 167 | + |
| 168 | +- `createCollection() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#createCollection(java.lang.String)>`__ |
| 169 | +- `listCollections() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#listCollections()>`__ |
| 170 | +- `insertOne() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#insertOne(TDocument)>`__ |
| 171 | +- `insertMany() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#insertMany(java.util.List)>`__ |
0 commit comments