Time Series Collections
On this page
Overview
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
Create a Time Series Collection
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:
Create a
TimeseriesOptions
instance that specifies properties of your time series collection.Call the
create_collection()
method and pass the collection name as a parameter.Chain the
timeseries()
method to thecreate_collection()
method. Pass yourTimeseriesOptions
instance as a parameter totimeseries()
.
Example
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(); db.create_collection("sept2023") .timeseries(ts_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().await?; while let Some(c) = coll_list.try_next().await? { println!("{:#?}", c); }
CollectionSpecification { name: "sept2023", collection_type: Timeseries, options: CreateCollectionOptions { ... timeseries: Some( TimeseriesOptions { time_field: "precipitation_mm", meta_field: Some( "location", ), granularity: Some( Minutes, ), }, ), ... }, ... }
Query a Time Series Collection
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.
Additional Information
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.
API Documentation
To learn more about the methods and types mentioned in this guide, see the following API documentation: