|
| 1 | +.. _rust-time-series: |
| 2 | + |
| 3 | +======================= |
| 4 | +Time Series Collections |
| 5 | +======================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to use the {+driver-short+} to create |
| 17 | +and interact with **time series collections**. Time series collections |
| 18 | +efficiently store chronological sequences of measurements over a period |
| 19 | +of time. Each document in a time series collection contains the |
| 20 | +following pieces of information: |
| 21 | + |
| 22 | +- Quantity that is being measured over time |
| 23 | +- Metadata that describes the measurement |
| 24 | +- Timestamp for the measurement |
| 25 | + |
| 26 | +The following table describes some sample situations for which data could be |
| 27 | +stored in a time series collection. Each row describes the situation, |
| 28 | +the measured quantity, and the metadata in each document: |
| 29 | + |
| 30 | +.. list-table:: |
| 31 | + :widths: 40, 30, 30 |
| 32 | + :header-rows: 1 |
| 33 | + :stub-columns: 1 |
| 34 | + |
| 35 | + * - Situation |
| 36 | + - Measured Quantity |
| 37 | + - Metadata |
| 38 | + |
| 39 | + * - Recording monthly sales by industry |
| 40 | + - Revenue in USD |
| 41 | + - Company, country |
| 42 | + |
| 43 | + * - Tracking weather changes |
| 44 | + - Precipitation level |
| 45 | + - Location, sensor type |
| 46 | + |
| 47 | + * - Recording fluctuations in housing prices |
| 48 | + - Monthly rent price |
| 49 | + - Location, currency |
| 50 | + |
| 51 | +This guide includes the following sections: |
| 52 | + |
| 53 | +- :ref:`Create a Time Series Collection <rust-tseries-create>` describes |
| 54 | + the syntax for creating a time series collection and provides example code |
| 55 | + |
| 56 | +- :ref:`Query a Time Series Collection <rust-tseries-query>` describes how to |
| 57 | + perform operations on time series collections |
| 58 | + |
| 59 | +- :ref:`Additional Information <rust-tseries-addtl-info>` |
| 60 | + provides links to resources and API documentation for types |
| 61 | + and methods mentioned in this guide |
| 62 | + |
| 63 | +.. _rust-tseries-create: |
| 64 | + |
| 65 | +Create a Time Series Collection |
| 66 | +------------------------------- |
| 67 | + |
| 68 | +.. important:: Server Version for Time Series Collections |
| 69 | + |
| 70 | + To create and interact with time series collections, you must be |
| 71 | + connected to a deployment running MongoDB 5.0 or later. |
| 72 | + |
| 73 | + |
| 74 | +To create a time series collection, perform the following actions: |
| 75 | + |
| 76 | +1. Create a ``TimeseriesOptions`` instance that specifies properties of |
| 77 | + your time series collection. |
| 78 | + |
| 79 | +#. Create a ``CreateCollectionOptions`` instance and set the value of |
| 80 | + the ``timeseries`` field to your ``TimeseriesOptions`` instance. |
| 81 | + |
| 82 | +#. Pass the ``CreateCollectionOptions`` instance to the |
| 83 | + ``create_collection()`` method. You must also pass the collection |
| 84 | + name as a parameter. |
| 85 | + |
| 86 | +Example |
| 87 | +~~~~~~~ |
| 88 | + |
| 89 | +This example creates the ``sept2023`` time series collection in the |
| 90 | +``precipitation`` database with the following configuration: |
| 91 | + |
| 92 | +- ``time_field`` is set to ``"precipitation_mm"`` |
| 93 | +- ``meta_field`` is set to ``"location"`` |
| 94 | +- ``granularity`` is set to minutes |
| 95 | + |
| 96 | +.. literalinclude:: /includes/fundamentals/code-snippets/tseries.rs |
| 97 | + :start-after: begin-create-ts |
| 98 | + :end-before: end-create-ts |
| 99 | + :language: rust |
| 100 | + :dedent: |
| 101 | + |
| 102 | +To verify that you successfully created the time series collection, run |
| 103 | +the ``list_collections()`` method on the database and print the results: |
| 104 | + |
| 105 | +.. io-code-block:: |
| 106 | + :copyable: true |
| 107 | + |
| 108 | + .. input:: /includes/fundamentals/code-snippets/tseries.rs |
| 109 | + :start-after: begin-list-colls |
| 110 | + :end-before: end-list-colls |
| 111 | + :language: rust |
| 112 | + :dedent: |
| 113 | + |
| 114 | + .. output:: |
| 115 | + :language: console |
| 116 | + :visible: false |
| 117 | + |
| 118 | + CollectionSpecification { |
| 119 | + name: "sept2023", |
| 120 | + collection_type: Timeseries, |
| 121 | + options: CreateCollectionOptions { |
| 122 | + ... |
| 123 | + timeseries: Some( |
| 124 | + TimeseriesOptions { |
| 125 | + time_field: "precipitation_mm", |
| 126 | + meta_field: Some( |
| 127 | + "location", |
| 128 | + ), |
| 129 | + granularity: Some( |
| 130 | + Minutes, |
| 131 | + ), |
| 132 | + }, |
| 133 | + ), |
| 134 | + ... |
| 135 | + }, |
| 136 | + ... |
| 137 | + } |
| 138 | + |
| 139 | +.. _rust-tseries-query: |
| 140 | + |
| 141 | +Query a Time Series Collection |
| 142 | +------------------------------ |
| 143 | + |
| 144 | +You can use the same syntax and conventions to query a time series |
| 145 | +collection as you use when performing read or aggregation operations on |
| 146 | +other collections. To find more information about these operations, see |
| 147 | +the :ref:`Additional Information <rust-tseries-addtl-info>` section. |
| 148 | + |
| 149 | +.. _rust-tseries-addtl-info: |
| 150 | + |
| 151 | +Additional Information |
| 152 | +---------------------- |
| 153 | + |
| 154 | +To learn more about the concepts mentioned in this guide, see the |
| 155 | +following Server manual entries: |
| 156 | + |
| 157 | +- :manual:`Time Series </core/timeseries-collections/>` |
| 158 | +- :manual:`Create and Query a Time Series Collection </core/timeseries/timeseries-procedures/>` |
| 159 | +- :manual:`Set Granularity for Time Series Data </core/timeseries/timeseries-granularity/>` |
| 160 | + |
| 161 | +To learn more about creating collections, see the guide on |
| 162 | +:ref:`rust-db-coll`. |
| 163 | + |
| 164 | +To learn more about performing read operations, see the guides in the |
| 165 | +:ref:`rust-crud-read-operations` category. |
| 166 | + |
| 167 | +API Documentation |
| 168 | +~~~~~~~~~~~~~~~~~ |
| 169 | + |
| 170 | +To learn more about the methods and types mentioned in this |
| 171 | +guide, see the following API documentation: |
| 172 | + |
| 173 | +- `TimeseriesOptions <{+api+}/options/struct.TimeseriesOptions.html>`__ |
| 174 | +- `CreateCollectionOptions <{+api+}/options/struct.CreateCollectionOptions.html>`__ |
| 175 | +- `create_collection() <{+api+}/struct.Database.html#method.create_collection>`__ |
| 176 | +- `TimeseriesGranularity <{+api+}/options/enum.TimeseriesGranularity.html>`__ |
| 177 | +- `list_collections() <{+api+}/struct.Database.html#method.list_collections>`__ |
| 178 | +- `CollectionSpecification <{+api+}/results/struct.CollectionSpecification.html>`__ |
0 commit comments