On this page本页内容
New in version 5.0.在版本5.0中新增。
Time series collections时间序列集合 efficiently store sequences of measurements over a period of time. 有效地存储一段时间内的测量序列。Time series data is any data that is collected over time and is uniquely identified by one or more unchanging parameters. 时间序列数据是随时间集合的,由一个或多个不变参数唯一标识的任何数据。The unchanging parameters that identify your time series data is generally your data source's metadata.识别时间序列数据的不变参数通常是数据源的元数据。
URL |
Compared to normal collections, storing time series data in time series collections improves query efficiency and reduces the disk usage for time series data and secondary indexes.与正常集合相比,在时间序列集合中存储时间序列数据可以提高查询效率,并减少时间序列数据和二级索引的磁盘使用。
You can only create time series collections on a system with featureCompatibilityVersion set to 5.0.只能在featureCompatibilityVersion设置为5.0的系统上创建时间序列集合。
Before you can insert data into a time series collection, you must explicitly create the collection using either the 在将数据插入时间序列集合之前,必须使用db.createCollection()
method or the create
command:db.createCollection()
方法或create
命令显式创建集合:
db.createCollection( "weather", { timeseries: { timeField: "timestamp", metaField: "metadata", granularity: "hours" } } )
When creating a time series collection, specify the following options:创建时间序列集合时,请指定以下选项:
timeseries.timeField | string |
|
timeseries.metaField | string |
|
timeseries.granularity | string |
|
expireAfterSeconds | number |
Other options allowed with the timeseries
option are:timeseries
选项允许的其他选项包括:
storageEngine
indexOptionDefaults
collation
writeConcern
comment
db.createCollection()
and 和create
.。
Each document you insert should contain a single measurement. 插入的每个文档都应包含一个测量值。To insert multiple documents at once, issue the following command:要同时插入多个文档,请发出以下命令:
db.weather.insertMany( [ { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-18T00:00:00.000Z"), "temp": 12 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-18T04:00:00.000Z"), "temp": 11 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-18T08:00:00.000Z"), "temp": 11 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-18T12:00:00.000Z"), "temp": 12 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-18T16:00:00.000Z"), "temp": 16 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-18T20:00:00.000Z"), "temp": 15 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-19T00:00:00.000Z"), "temp": 13 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-19T04:00:00.000Z"), "temp": 12 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-19T08:00:00.000Z"), "temp": 11 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-19T12:00:00.000Z"), "temp": 12 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-19T16:00:00.000Z"), "temp": 17 }, { "metadata": { "sensorId": 5578, "type": "temperature" }, "timestamp": ISODate("2021-05-19T20:00:00.000Z"), "temp": 12 } ] )
To insert a single document, use the 要插入单个文档,请使用db.collection.insertOne()
method.db.collection.insertOne()
方法。
To retrieve one document from a time series collection, issue the following command:要从时间序列集合中检索一个文档,请发出以下命令:
db.weather.findOne({ "timestamp": ISODate("2021-05-18T00:00:00.000Z") })
For additional query functionality, use an aggregation pipeline such as:要获得其他查询功能,请使用聚合管道,例如:
db.weather.aggregate( [ { $project: { date: { $dateToParts: { date: "$timestamp" } }, temp: 1 } }, { $group: { _id: { date: { year: "$date.year", month: "$date.month", day: "$date.day" } }, avgTmp: { $avg: "$temp" } } } ] )
The example aggregation pipeline groups all documents by the date of the measurement and then returns the average of all temperature measurements that day:示例聚合管道按测量日期对所有文档进行分组,然后返回当天所有温度测量的平均值:
{ "_id" : { "date" : { "year" : 2021, "month" : 5, "day" : 18 } }, "avgTmp" : 12.714285714285714 } { "_id" : { "date" : { "year" : 2021, "month" : 5, "day" : 19 } }, "avgTmp" : 13 }
To determine if a collection is of type time series, use the listCollections command:要确定集合是否为时间序列类型,请使用listCollections命令:
db.runCommand( { listCollections: 1.0 } )
If the collection is a time series collection, it returns this:如果集合是时间序列集合,它将返回以下内容:
{ cursor: { id: <number>, ns: 'test.$cmd.listCollections', firstBatch: [ { name: <string>, type: 'timeseries', options: { expireAfterSeconds: <number>, timeseries: { ... } }, ... }, ... ] } }
Time series collections behave like normal collections. 时间序列集合的行为与普通集合类似。You can insert and query your data as you normally would. 您可以像往常一样插入和查询数据。MongoDB treats time series collections as writable non-materialized views on internal collections that automatically organize time series data into an optimized storage format on insert.MongoDB将时间序列集合视为内部集合上的可写非物化视图,可在插入时自动将时间序列数据组织为优化的存储格式。
When you query time series collections, you operate on one document per measurement. 当您查询时间序列集合时,每个度量操作一个文档。Queries on time series collections take advantage of the optimized internal storage format and return results faster.对时间序列集合的查询利用了优化的内部存储格式,并更快地返回结果。
The implementation of time series collections uses internal collections that reduce disk usage and improve query efficiency. 时间序列集合的实现使用内部集合来减少磁盘使用并提高查询效率。Time series collections automatically order and index data by time. 时间序列集合自动按时间排序和索引数据。The internal index for a time series collection is not displayed by listIndexes
.listIndexes
不显示时间序列集合的内部索引。
To improve query performance, you can manually add secondary indexes on the fields specified as the 为了提高查询性能,可以在指定为metaField
and the timeField
.metaField
和timeField
的字段上手动添加二级索引。
Time series collections ignore the global default compression algorithm, snappy, in favor of zstd, unless a different compression algorithm is specified using the 时间序列集合忽略全局默认压缩算法snappy,而支持zstd,除非在创建集合时使用storageEngine选项指定了不同的压缩算法。storageEngine
option when the collection was created. For example, to change the compression algorithm to 例如,要将新snappy
for a new weather
collection, add the following option:weather
集合的压缩算法更改为snappy
,请添加以下选项:
db.createCollection( "weather", { timeseries: { timeField: "timestamp" }, storageEngine: { wiredTiger: { configString: "block_compressor=snappy" } } } )
Valid 有效的block_compressor
options are:block_compressor
选项包括:
snappy
zlib
zstd
(default)none
Starting in MongoDB 5.2, time series collection data is further compressed to save database space. 从MongoDB 5.2开始,时间序列采集数据被进一步压缩以节省数据库空间。This compression does not affect query results, nor does it negatively affect performance.这种压缩不会影响查询结果,也不会对性能产生负面影响。