A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/languages/python/pymongo-driver/current/data-formats/time-series/ below:

Time Series Data - PyMongo Driver v4.13

In this guide, you can learn how to use PyMongo to store and interact with time series data.

Time series data is composed of the following components:

The following table describes sample situations for which you could store time series data:

Situation

Measured Quantity

Metadata

Recording monthly sales by industry

Revenue in USD

Company, country

Tracking weather changes

Precipitation level

Location, sensor type

Recording fluctuations in housing prices

Monthly rent price

Location, currency

Important Server Version for Time Series Collections

To create and interact with time series collections, you must be connected to a deployment running MongoDB Server 5.0 or later.

To create a time series collection, pass the following arguments to the create_collection() method:

The timeseries argument is of type dict. It contains the following fields:

See Command Fields to learn more about these fields.

The following example creates a time series collection named october2024 with the timeField option set to "timestamp". Select the Synchronous or Asynchronous tab to see the corresponding code:

database = client.get_database("weather")time_series_options = {    "timeField": "timestamp"}database.create_collection("october2024", timeseries=time_series_options)
database = client.get_database("weather")time_series_options = {    "timeField": "timestamp"}await database.create_collection("october2024", timeseries=time_series_options)

To check if you successfully created the collection, you can get a list of all collections in your database and filter by collection name. Select the Synchronous or Asynchronous tab to see the corresponding code:

print(list(database.list_collections(filter={'name': 'october2024'})))
{    "name": "october2024",    "type": "timeseries",    "options": {        "timeseries":   {            "timeField": "timestamp",            "granularity": "seconds",            "bucketMaxSpanSeconds": 3600        }    },    "info": {        "readOnly": False    }}
collections = await database.list_collections(filter={'name': 'october2024'})print(await collections.to_list())
{    "name": "october2024",    "type": "timeseries",    "options": {        "timeseries":   {            "timeField": "timestamp",            "granularity": "seconds",            "bucketMaxSpanSeconds": 3600        }    },    "info": {        "readOnly": False    }}

You can insert data into a time series collection by using the insert_one() or insert_many() methods and specifying the measurement, timestamp, and metadata in each inserted document.

To learn more about inserting documents, see Insert Documents.

This example inserts New York City temperature data into the october2024 time series collection created in Create a Time Series Collection. Each document contains the following fields:

Select the Synchronous or Asynchronous tab to see the corresponding code:

from datetime import datetimecollection = database["october2024"]document_list = [    { "temperature": 77, "location": "New York City", "timestamp": datetime(2024, 10, 22, 6, 0, 0) },    { "temperature": 74, "location": "New York City", "timestamp": datetime(2024, 10, 23, 6, 0, 0) }]collection.insert_many(document_list)
from datetime import datetimecollection = database["october2024"]document_list = [    { "temperature": 77, "location": "New York City", "timestamp": datetime(2024, 10, 22, 6, 0, 0) },    { "temperature": 74, "location": "New York City", "timestamp": datetime(2024, 10, 23, 6, 0, 0) }]await collection.insert_many(document_list)
Tip Formatting Dates and Times

To learn more about using datetime objects in PyMongo, see Dates and Times.

You can use the same syntax and conventions to query data stored in a time series collection as you use when performing read or aggregation operations on other collections. To learn more about these operations, see Query and Transform Your Data with Aggregation.

To learn more about the concepts in this guide, see the following MongoDB Server manual entries:

To learn more about the methods mentioned in this guide, see the following API documentation:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4