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
. timeField
和metaField
的字段上创建一个或多个复合索引。If the field value for the 如果metaField
field is a document, you can create secondary indexes on fields inside that document.metaField
字段的字段值是文档,则可以在该文档内的字段上创建二级索引。
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.sensorId
和timestamp
字段上创建复合索引:
db.weather24h.createIndex({ "metadata.sensorId": 1, "timestamp": 1 })
To force MongoDB to use a specific index when querying a time series collection, use 要强制MongoDB在查询时间序列集合时使用特定索引,请使用带有索引名称的cursor.hint()
with an index name: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()
方法。