|
| 1 | +.. _pymongo-time-series: |
| 2 | + |
| 3 | +================ |
| 4 | +Time Series Data |
| 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 {+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 | +.. _pymongo-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 | +To create a time series collection, pass the following arguments to the |
| 60 | +``create_collection()`` method: |
| 61 | + |
| 62 | +- Name of the new collection to create |
| 63 | +- ``timeseries`` argument |
| 64 | + |
| 65 | +The ``timeseries`` argument is of type ``dict``. It contains the following fields: |
| 66 | + |
| 67 | +- ``timeField``: Specifies the field that stores a timestamp in each time series |
| 68 | + document. |
| 69 | +- ``metaField``: Specifies the field that stores metadata in each time series |
| 70 | + document. |
| 71 | +- ``granularity``: Specifies the approximate time between consecutive timestamps. |
| 72 | + The possible values are ``'seconds'``, ``'minutes'``, and ``'hours'``. |
| 73 | +- ``bucketMaxSpanSeconds``: Sets the maximum time between timestamps in the |
| 74 | + same bucket. |
| 75 | +- ``bucketRoundingSeconds``: Sets the number of seconds to round down by when |
| 76 | + MongoDB sets the minimum timestamp for a new bucket. Must be equal to |
| 77 | + ``bucketMaxSpanSeconds``. |
| 78 | + |
| 79 | +See :manual:`Command Fields </reference/command/create/#command-fields>` |
| 80 | +to learn more about these fields. |
| 81 | + |
| 82 | +Example |
| 83 | +~~~~~~~ |
| 84 | + |
| 85 | +The following example creates a time series collection named ``october2024`` with the |
| 86 | +``timeField`` option set to ``"timestamp"``: |
| 87 | + |
| 88 | +.. code-block:: python |
| 89 | + |
| 90 | + database = client.get_database("weather") |
| 91 | + |
| 92 | + time_series_options = { |
| 93 | + "timeField": "timestamp" |
| 94 | + } |
| 95 | + |
| 96 | + database.create_collection("october2024", timeseries=time_series_options) |
| 97 | + |
| 98 | +To check if you successfully created the collection, you can get a list of all |
| 99 | +collections in your database and filter by collection name: |
| 100 | + |
| 101 | +.. io-code-block:: |
| 102 | + :copyable: true |
| 103 | + |
| 104 | + .. input:: |
| 105 | + :language: python |
| 106 | + |
| 107 | + print(list(database.list_collections(filter={'name': 'october2024'}))) |
| 108 | + |
| 109 | + .. output:: |
| 110 | + :language: json |
| 111 | + :visible: false |
| 112 | + |
| 113 | + { |
| 114 | + "name": "october2024", |
| 115 | + "type": "timeseries", |
| 116 | + "options": { |
| 117 | + "timeseries": { |
| 118 | + "timeField": "timestamp", |
| 119 | + "granularity": "seconds", |
| 120 | + "bucketMaxSpanSeconds": 3600 |
| 121 | + } |
| 122 | + }, |
| 123 | + "info": { |
| 124 | + "readOnly": False |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +.. _pymongo-time-series-write: |
| 129 | + |
| 130 | +Store Time Series Data |
| 131 | +---------------------- |
| 132 | + |
| 133 | +You can insert data into a time series collection by using the ``insert_one()`` |
| 134 | +or ``insert_many()`` methods and specifying the measurement, timestamp, and |
| 135 | +metadata in each inserted document. |
| 136 | + |
| 137 | +To learn more about inserting documents, see :ref:`pymongo-write-insert`. |
| 138 | + |
| 139 | +Example |
| 140 | +~~~~~~~ |
| 141 | + |
| 142 | +This example inserts New York City temperature data into the ``october2024`` |
| 143 | +time series collection created in :ref:`pymongo-time-series-create`. Each |
| 144 | +document contains the following fields: |
| 145 | + |
| 146 | +- ``temperature``, which stores temperature measurements in degrees Fahrenheit |
| 147 | +- ``location``, which stores location metadata |
| 148 | +- ``timestamp``, which stores the measurement timestamp |
| 149 | + |
| 150 | +.. code-block:: python |
| 151 | + |
| 152 | + from datetime import datetime |
| 153 | + |
| 154 | + collection = database["october2024"] |
| 155 | + |
| 156 | + document_list = [ |
| 157 | + { "temperature": 77, "location": "New York City", "timestamp": datetime(2024, 10, 22, 6, 0, 0) }, |
| 158 | + { "temperature": 74, "location": "New York City", "timestamp": datetime(2024, 10, 23, 6, 0, 0) } |
| 159 | + ] |
| 160 | + |
| 161 | + collection.insert_many(document_list) |
| 162 | + |
| 163 | +.. tip:: Formatting Dates and Times |
| 164 | + |
| 165 | + To learn more about using ``datetime`` objects in {+driver-short+}, see |
| 166 | + :ref:`pymongo-dates-times`. |
| 167 | + |
| 168 | +.. _pymongo-time-series-read: |
| 169 | + |
| 170 | +Query Time Series Data |
| 171 | +---------------------- |
| 172 | + |
| 173 | +You can use the same syntax and conventions to query data stored in a time |
| 174 | +series collection as you use when performing read or aggregation operations on |
| 175 | +other collections. To learn more about these operations, see :ref:`pymongo-read` |
| 176 | +and :ref:`pymongo-aggregation`. |
| 177 | + |
| 178 | +.. _pymongo-time-series-addtl-info: |
| 179 | + |
| 180 | +Additional Information |
| 181 | +---------------------- |
| 182 | + |
| 183 | +To learn more about the concepts in this guide, see the following {+mdb-server+} |
| 184 | +manual entries: |
| 185 | + |
| 186 | +- :manual:`Time Series </core/timeseries-collections/>` |
| 187 | +- :manual:`Create and Query a Time Series Collection </core/timeseries/timeseries-procedures/>` |
| 188 | +- :manual:`Set Granularity for Time Series Data </core/timeseries/timeseries-granularity/>` |
| 189 | + |
| 190 | +API Documentation |
| 191 | +~~~~~~~~~~~~~~~~~ |
| 192 | + |
| 193 | +To learn more about the methods mentioned in this guide, see the following |
| 194 | +API documentation: |
| 195 | + |
| 196 | +- `create_collection() <https://pymongo.readthedocs.io/en/4.10.1/api/pymongo/database.html#pymongo.database.Database.create_collection>`__ |
| 197 | +- `list_collections() <https://pymongo.readthedocs.io/en/4.10.1/api/pymongo/database.html#pymongo.database.Database.list_collections>`__ |
| 198 | +- `insert_one() <https://pymongo.readthedocs.io/en/4.10.1/api/pymongo/collection.html#pymongo.collection.Collection.insert_one>`__ |
| 199 | +- `insert_many() <https://pymongo.readthedocs.io/en/4.10.1/api/pymongo/collection.html#pymongo.collection.Collection.insert_many>`__ |
0 commit comments