About Querying Time Series Data
MongoDB groups documents with matching metaFields to optimize the storage and query
latency of time series data. Your choice of metaField
has the biggest
impact on optimizing queries in your application.
Querying the metaField
You query a time series collection the same way you query a standard MongoDB collection. For an example query and example aggregation pipeline, see 查询时间序列集合. For a list of querying best practices, see Query Best Practices.
Queries against time series data typically focus on a single time series in the collection. For example, consider a time series collection that tracks stock data using the following schema:
{ _id: 573a1397f29313caabce8347, "ticker": "MDB", "timestamp": ISODate("2024-07-24T13:45:00.000Z"), "price": 248.21, "volume": 6930 }
The collection has the following settings:
timeseries: { timeField: "timestamp", metaField: "ticker", granularity: "seconds" }
MongoDB groups documents with matching ticker
values. Instead of having to
check for matches across all fields in all documents, the server only has to
check against the metaField
, in this case ticker
, to narrow down
the search range to a unique time series. This fits the expected use
case, which is searching for activity on a single stock. A user searching for
information on MongoDB stock (MDB) doesn't need to consider results for Amazon
(AMZN).
Querying the timeField
The second major dimension for querying time series data is time. Since MongoDB
groups documents that have both an identical metaField
value and close
timeField
values, this further narrows the scope of a query to a range of
buckets. Recent transactions are kept in memory, so it's easy to stream data in
real time.
块处理
从版本 8.0 开始,MongoDB 可以使用块处理来执行某些时间序列查询。这一性能改进利用数据“块”而不是单个值来处理查询。当使用时间序列集合时,块处理可提高查询执行速度和吞吐量。
Block processing significantly improves performance and reduces overhead for long running aggregation pipelines that begin with the following stages:
MongoDB automatically enables block processing for eligible time series queries. You cannot manually specify whether a query uses block processing.
To see if your time series query uses block processing, see
stages
in the
explain plan output.