Docs Menu
Docs Home
/ / /
Rust Driver
/

Time Series Collections

On this page

  • Overview
  • Create a Time Series Collection
  • Example
  • Query a Time Series Collection
  • Additional Information
  • API Documentation

In this guide, you can learn how to use the Rust driver to create and interact with time series collections. Time series collections efficiently store chronological sequences of measurements over a period of time. Each document in a time series collection contains the following pieces of information:

  • Quantity that is being measured over time

  • Metadata that describes the measurement

  • Timestamp for the measurement

The following table describes some sample situations for which data could be stored in a time series collection. Each row describes the situation, the measured quantity, and the metadata in each document:

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

This guide includes the following sections:

  • Create a Time Series Collection describes the syntax for creating a time series collection and provides example code

  • Query a Time Series Collection describes how to perform operations on time series collections

  • Additional Information provides links to resources and API documentation for types and methods mentioned in this guide

Important

Server Version for Time Series Collections

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

To create a time series collection, perform the following actions:

  1. Create a TimeseriesOptions instance that specifies properties of your time series collection.

  2. Create a CreateCollectionOptions instance and set the value of the timeseries field to your TimeseriesOptions instance.

  3. Pass the CreateCollectionOptions instance to the create_collection() method. You must also pass the collection name as a parameter.

This example creates the sept2023 time series collection in the precipitation database with the following configuration:

  • time_field is set to "precipitation_mm"

  • meta_field is set to "location"

  • granularity is set to minutes

let db = client.database("precipitation");
let ts_opts = TimeseriesOptions::builder()
.time_field("precipitation_mm".to_string())
.meta_field(Some("location".to_string()))
.granularity(Some(TimeseriesGranularity::Minutes))
.build();
let coll_opts = CreateCollectionOptions::builder()
.timeseries(ts_opts)
.build();
db.create_collection("sept2023", coll_opts).await?;

To verify that you successfully created the time series collection, run the list_collections() method on the database and print the results:

let mut coll_list = db.list_collections(None, None).await?;
while let Some(c) = coll_list.try_next().await? {
println!("{:#?}", c);
}

You can use the same syntax and conventions to query a time series collection as you use when performing read or aggregation operations on other collections. To find more information about these operations, see the Additional Information section.

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

To learn more about creating collections, see the guide on Databases and Collections.

To learn more about performing read operations, see the guides in the Read Operations category.

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

← Transactions