Add Secondary Indexes on metaField and timeField在元域和时间域上添加二级索引

On this page本页内容

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. 具体来说,我们建议您在指定为timeFieldmetaField的字段上创建一个或多个复合索引If the field value for the metaField field is a document, you can create secondary indexes on fields inside that document.如果metaField字段的字段值是文档,则可以在该文档内的字段上创建二级索引。

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:例如,此命令在metadata.sensorIdtimestamp字段上创建复合索引

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

Index Hints for Time Series Collections时间序列集合的索引提示

To force MongoDB to use a specific index when querying a time series collection, use cursor.hint() with an index name:要强制MongoDB在查询时间序列集合时使用特定索引,请使用带有索引名称的cursor.hint()

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.要获取集合上索引的名称,请使用db.collection.getIndexes()方法。

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