Docs Menu
Docs Home
/ /

Create and Query a Time Series Collection

This page shows how to create and query a time series collection. The code examples provided create and work with sample weather data that contains time, temperature, and sensor information.

Important

Feature Compatibility Version Requirement

You can only create time series collections on a system with featureCompatibilityVersion set to 5.0 or greater.

A time series collection includes the following fields:

Field
Type
Description

timeseries.timeField

string

Required. The name of the field which contains the date in each time series document. Documents in a time series collection must have a valid BSON date as the value for the timeField.

timeseries.metaField

string

Optional. The name of the field which contains metadata in each time series document. The metadata in the specified field should be data that is used to label a unique series of documents. The metadata should rarely, if ever, change The name of the specified field may not be _id or the same as the timeseries.timeField. The field can be of any data type.

Although the metaField field is optional, using metadata can improve query optimization. For example, MongoDB automatically creates a compound index on the metaField and timeField fields for new collections. If you do not provide a value for this field, the data is bucketed solely based on time.

timeseries.granularity

integer

Optional. Do not use if setting bucketRoundingSeconds and bucketMaxSpanSeconds.

Possible values are seconds (default), minutes, and hours.

Set granularity to the value that most closely matches the time between consecutive incoming timestamps. This improves performance by optimizing how MongoDB stores data in the collection.

For more information on granularity and bucket intervals, see Set Granularity for Time Series Data.

timeseries.bucketMaxSpanSeconds

integer

Optional. Use with bucketRoundingSeconds as an alternative to granularity. Sets the maximum time between timestamps in the same bucket.

Possible values are 1-31536000.

New in version 6.3.

timeseries.bucketRoundingSeconds

integer

Optional. Use with bucketMaxSpanSeconds as an alternative to granularity. Must be equal to bucketMaxSpanSeconds.

When a document requires a new bucket, MongoDB rounds down the document's timestamp value by this interval to set the minimum time for the bucket.

New in version 6.3.

expireAfterSeconds

integer

Optional. Enable the automatic deletion of documents in a time series collection by specifying the number of seconds after which documents expire. MongoDB deletes expired documents automatically. See Set up Automatic Removal for Time Series Collections (TTL) for more information.

Other allowed options that are not specific to time series collections are:

  • storageEngine

  • indexOptionDefaults

  • collation

  • writeConcern

  • comment

Back

Create & Configure

On this page