Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Add Secondary Indexes on metaField and timeField

On this page

  • Index Hints for Time Series Collections

To improve query performance for time series collections, add one or more secondary indexes to support common time series query patterns. Specifically, we recommend that you create one or more compound indexes on the fields specified as the timeField and the metaField. If the field value for the metaField field is a document, you can create secondary indexes on fields inside that document.

Note

Not all index types are supported. For a list of unsupported index types, see Limitations for Secondary Indexes on Time Series Collections.

For example, this command creates a compound index on the metadata.sensorId and timestamp fields:

db.weather24h.createIndex({ "metadata.sensorId": 1, "timestamp": 1 })

Tip

See:

To force MongoDB to use a specific index when querying a time series collection, use cursor.hint() with an index name:

db.weather24h.find({ "metadata.sensorId": 1235 }).hint("metadata.sensorId_1_timestamp_1")

On a time series collection, you can only specify hints using the index name, not the index key pattern. To get the names of the indexes on a collection, use the db.collection.getIndexes() method.

←  Set Granularity for Time Series DataMigrate Data into a Time Series Collection →