Create and Query a Time Series Collection创建和查询时间序列集合
On this page本页内容
This page shows how to create and query a time series collection, with code examples.此页面显示如何创建和查询时间序列集合,并提供代码示例。
Feature Compatibility Version Requirement功能兼容性版本要求
You can only create time series collections on a system with featureCompatibilityVersion set to 5.0 or greater.只能在featureCompatibilityVersion设置为5.0或更高版本的系统上创建时间序列集合。
Create a Time Series Collection创建时间序列集合
Create the collection using either the 使用db.createCollection()
method or the create
command. For example:db.createCollection()
方法或create
命令创建集合。例如
db.createCollection(
"weather",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata"
}})
Set the 将timeField
to the field that contains time data, and the metaField
to the field that contains metadata:timeField
设置为包含时间数据的字段,将metaField
设置为含有元数据的字段:
timeseries: {
timeField: "timestamp",
metaField: "metadata"
}
Define the time interval for each bucket of data using one of the two approaches below. 使用以下两种方法之一定义每个数据桶的时间间隔。For more detailed information, see Set Granularity for Time Series Data.有关更多详细信息,请参阅设置时间序列数据的粒度。
Define a定义granularity
field:granularity
字段:timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "seconds"
}
OR
In MongoDB 6.3 and higher, you can define在MongoDB 6.3及更高版本中,您可以定义bucketMaxSpanSeconds
andbucketRoundingSeconds
fields. Both values must be the same:bucketMaxSpanSeconds
和bucketRoundingSeconds
字段。两个值必须相同:timeseries: {
timeField: "timestamp",
metaField: "metadata",
bucketMaxSpanSeconds: "300",
bucketRoundingSeconds: "300"
}
Optionally, set (可选)将expireAfterSeconds
to expire documents when the value of the timeField
is at least that old:expireAfterSeconds
设置为在timeField
的值至少为以下值时使文档过期:
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "seconds",
expireAfterSeconds: "86400"
}
Time Series Field Reference时间序列字段参考
A time series collection includes the following fields:时间序列集合包括以下字段:
timeseries.timeField | string | timeField .timeField 的值。
|
timeseries.metaField | string | _id or the same as the timeseries.timeField . The field can be of any type._id ,也不能与timeseries.timeField 相同。字段可以是任何类型。
|
timeseries.granularity | integer | bucketRoundingSeconds and bucketMaxSpanSeconds .bucketRoundingSeconds 和bucketMaxSpanSeconds ,则不要使用。seconds (default), minutes , and hours .seconds (默认值)、minutes 和hours 。granularity to the value that most closely matches the time between consecutive incoming timestamps. granularity 设置为与连续传入时间戳之间的时间最匹配的值。 |
timeseries.bucketMaxSpanSeconds | integer | bucketRoundingSeconds as an alternative to granularity . Sets the maximum time between timestamps in the same bucket.bucketRoundingSeconds 一起使用作为granularity 的替代方法。设置同一桶中时间戳之间的最长时间。 |
timeseries.bucketRoundingSeconds | integer | bucketMaxSpanSeconds as an alternative to granularity . Must be equal to bucketMaxSpanSeconds .bucketMaxSpanSeconds 一起使用,作为粒度的替代方案。必须等于bucketMaxSpanSeconds 。 |
timeseries.expireAfterSeconds | integer |
Other allowed options that are not specific to time series collections are:不特定于时间序列集合的其他允许选项包括:
storageEngine
indexOptionDefaults
collation
writeConcern
comment
See: 参阅:
db.createCollection()
and create
.db.createCollection()
和create
。
Insert Measurements into a Time Series Collection将测量值插入时间序列集合
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()
方法。
Optimize Insert Performance优化插入性能
To learn how to optimize inserts for large operations, see Optimize Inserts.要了解如何优化大型操作的插入,请参阅优化插入。
Query a Time Series Collection查询时间序列集合
You query a time series collection the same way you query a standard MongoDB collection.查询时间序列集合的方式与查询标准MongoDB集合的方式相同。
To return one document from a time series collection, run:要从时间序列集合返回一个文档,请运行:
db.weather.findOne({
"timestamp": ISODate("2021-05-18T00:00:00.000Z")
})
Example output:示例输出:
{
timestamp: ISODate("2021-05-18T00:00:00.000Z"),
metadata: { sensorId: 5578, type: 'temperature' },
temp: 12,
_id: ObjectId("62f11bbf1e52f124b84479ad")
}
For more information on time series queries, see Optimize Query Performance.有关时间序列查询的详细信息,请参阅优化查询性能。
Run Aggregations on a Time Series Collection在时间序列集合上运行聚合
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
}