Skip to content

Commit fe6391f

Browse files
committed
initial commit
1 parent 5f64e40 commit fe6391f

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed

source/data-formats.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Specialized Data Formats
2424
/data-formats/custom-types
2525
/data-formats/dates-and-times
2626
/data-formats/uuid
27+
/data-formats/time-series
2728

2829
Overview
2930
--------
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
.. _pymongo-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+
.. _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+
You can create a time series collection to store time series data. To create
60+
a time series collection, pass the following parameters to the ``create_collection()``
61+
method:
62+
63+
- Name of the new collection to create
64+
- ``timeseries`` argument
65+
66+
The ``timeseries`` argument is of type ``dict``. It contains the following fields:
67+
- ``timeField``
68+
- ``metaField``
69+
- ``granularity``
70+
- ``bucketMaxSpanSeconds``
71+
- ``bucketRoundingSeconds``
72+
73+
See `Command Fields <https://www.mongodb.com/docs/manual/reference/command/create/#command-fields>`__
74+
to learn more about what these fields mean.
75+
76+
The following example shows how to create a time series collection using the
77+
{+driver-short+}.
78+
79+
Example
80+
~~~~~~~
81+
82+
This example creates a time series collection named ``october2024`` with the
83+
``timeField`` option set to ``"timestamp"``.
84+
85+
.. code-block:: python
86+
87+
database = client.get_database("weather")
88+
89+
time_series_options = {
90+
"timeField": "timestamp"
91+
}
92+
93+
database.create_collection("october2024", timeseries=time_series_options)
94+
95+
To check if you successfully created the collection, you can print out a list of
96+
all collections in the database:
97+
98+
.. io-code-block::
99+
:copyable: true
100+
101+
.. input::
102+
:language: python
103+
104+
collection_list = database.list_collections()
105+
106+
for c in collection_list:
107+
print(c)
108+
109+
.. output::
110+
:language: json
111+
112+
{
113+
"name": "october2024",
114+
"type": "timeseries",
115+
"options": {
116+
"timeseries": {
117+
"timeField": "timestamp",
118+
"granularity": "seconds",
119+
"bucketMaxSpanSeconds": 3600
120+
}
121+
},
122+
"info": {
123+
"readOnly": False
124+
}
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:: ``datetime`` format
164+
165+
See :ref:`pymongo-dates-times` to learn more about using ``datetime``
166+
objects in {+driver-short+}.
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>`__
200+
201+
202+
203+
204+
205+

0 commit comments

Comments
 (0)