Time Series Collections
On this page
Overview
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 |
Create a Time Series Collection
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 aTimeSeriesOptions
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 } } ...
Query a Time Series Collection
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.
Additional Information
To learn more about the operations mentioned on this page, see the following Server manual guides:
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: