Docs Menu
Docs Home
/ / /
C#/.NET
/

Time Series Collections

On this page

  • Overview
  • Create a Time Series Collection
  • Query a Time Series Collection
  • Additional Information

In this guide, you can learn how to use and interact with time series collections in MongoDB using the MongoDB .NET/C# Driver.

Time series collections efficiently store sequences of measurements over a period of time. Time series data consists of any data collected over time, metadata that describes the measurement, and the time of the measurement.

Example
Measurement
Metadata
Sales Data
Revenue
Company
Infection Rates
Amount of People Infected
Location

Important

Time series collections require MongoDB 5.0 or later.

To create a time series collection, pass the following parameters to the CreateCollection() method:

  • Name of the new collection to create

  • CreateCollectionOptions object that contains a TimeSeriesOptions object

var database = mongoClient.GetDatabase("fall_weather");
var tsOptions = new TimeSeriesOptions("temperature");
// Creates a time series collection that stores "temperature" values over time
var collOptions = new CreateCollectionOptions { TimeSeriesOptions = tsOptions };
database.CreateCollection("september2021", collOptions);

To check if you successfully created the collection, use the ListCollections() or ListCollectionsAsync() method as shown in the following example:

var collections = database.ListCollections().ToList();
foreach (var collection in collections) {
Console.WriteLine(collection);
}

Your output will look similar to the following:

{
"name": "september2021",
"type": "timeseries",
"options": {
"timeseries": {
"timeField": "temperature",
"granularity": "seconds",
"bucketMaxSpanSeconds": 3600
}
},
"info": {
"readOnly": false
}
}
...

To query a time series collection, follow the conventions for retrieving and aggregating data. For more information about these conventions, see the Retrieve Data and Aggregation guides.

To learn more about the operations mentioned on this page, see the following Server manual guides:

  • Time Series Collections

  • Time Series Collection Limitations

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Logging