Docs Menu
Docs Home
MongoDB Manual
/ / /

Add Secondary Indexes on metaField and timeField

On this page

  • Use Secondary Indexes to Improve Sort Performance
  • Specify Index Hints for Time Series Collections
  • Time Series Secondary Indexes

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.


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 })



Sort operations on the timeField and metaField can use secondary indexes on those fields to improve performance.

For example, the following sensorData collection contains temperature readings:

db.sensorData.insertMany( [
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2022-01-15T00:00:00.000Z"),
"temperatureReading": 12
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2022-01-15T04:00:00.000Z"),
"temperatureReading": 11
"metadata": { "sensorId": 5579, "type": "temperature" },
"timestamp": ISODate("2022-01-15T08:00:00.000Z"),
"temperatureReading": 9
] )

The following command creates a compound ascending secondary index on the timestamp and metadata.sensorId fields:

{ "timestamp": 1, "metadata.sensorId": 1 }

The following sort operation on the timestamp field uses the index to improve performance:

db.sensorData.find().sort( { "timestamp": 1 } )

To confirm that the sort operation used the index, run the operation again with the .explain() option:

db.sensorData.find().sort( { "timestamp": 1 } ).explain()

The winningPlan.queryPlan.inputStage.stage is IXSCAN, which indicates that the index was used. For more information on explain plan output, see Explain Results.

Index hints cause MongoDB to use a specific index for a query. Some operations on time series collections can only take advantage of an index if that index is specified in a hint.

For example, the following query causes MongoDB to use the timestamp_1_metadata.sensorId_1 index:

db.sensorData.find( { "metadata.sensorId": 5578 } ).hint( "timestamp_1_metadata.sensorId_1" )

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

Starting in MongoDB 6.0 (and 5.0.16):


If there are secondary indexes on time series collections and you need to downgrade the feature compatibility version (fCV), you must first drop any secondary indexes that are incompatible with the downgraded fCV. See setFeatureCompatibilityVersion.

← Set Granularity for Time Series Data