This page shows how to create and query a time series collection, with code examples.本页通过代码示例展示了如何创建和查询时间序列集合。
Important
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创建时间序列集合
Define the 将timeField as the field that contains time data and the metaField as the field that contains metadata:timeField定义为包含时间数据的字段,将metaField定义为含有元数据的字段:
{
timeField: "timestamp",
metaField: "metadata"
}
In this example, 在这个例子中,timestamp is the name of the timeField and metadata is the name of the metaField. The value of the timestamp field must be a date type.timestamp是timeField的名称,metadata是metaField的名称。时间戳字段的值必须是日期类型。
Important
Choosing the right 为集合选择正确的metaField for your collection optimizes both storage and query performance. metaField可以优化存储和查询性能。For more information on 有关metaField selection and best practices, see metaFields.metaField选择和最佳实践的更多信息,请参阅metaFields。
Define the time interval for each bucket of data using one of the two approaches below:使用以下两种方法之一定义每个数据桶的时间间隔:
Important
Changing Time Series Intervals更改时间序列间隔
After creation, you can modify granularity or bucket definitions using the 创建后,您可以使用collMod method. However, you can only increase the time span covered by each bucket. You cannot decrease it.collMod方法修改粒度或桶定义。但是,您只能增加每个桶覆盖的时间跨度。你不能减少它。
Define a定义granularityfield:granularity字段:{
granularity: "seconds"
}For more detailed information on selecting a有关选择granularityvalue, see Granularity Considerations.granularity值的更多详细信息,请参阅粒度注意事项。
OR
In MongoDB 6.3 and later, you can define在MongoDB 6.3及更高版本中,您可以定义bucketMaxSpanSecondsandbucketRoundingSecondsfields. Both values must be the same:bucketMaxSpanSeconds和bucketRoundingSeconds字段。两个值必须相同:{
bucketMaxSpanSeconds: "300",
bucketRoundingSeconds: "300"
}
Optionally, set (可选)将expireAfterSeconds to expire documents when the value of the timeField is at least that old:expireAfterSeconds设置为当timeField的值至少为旧值时使文档过期:
{
expireAfterSeconds: 86400
}Create the collection using either the 使用db.createCollection() method or the create command. db.createCollection()方法或create命令创建集合。The follow example uses the 以下示例使用db.createCollection() method to create a weather time series collection:db.createCollection()方法创建weather时间序列集合:
db.createCollection(
"weather",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "seconds"
},
expireAfterSeconds: 86400
}
)Time Series Field Reference时间序列字段参考
A time series collection includes the following fields:时间序列集合包括以下字段:
timeseries.timeField | timeField.timeField的值。 | |
timeseries.metaField |
| |
timeseries.granularity |
| |
timeseries.bucketMaxSpanSeconds |
| |
timeseries.bucketRoundingSeconds |
| |
expireAfterSeconds |
Other allowed options that are not specific to time series collections are:其他不特定于时间序列集合的允许选项包括:
storageEngineindexOptionDefaultscollationwriteConcerncomment
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()方法。
Tip
Optimize Insert Performance优化插入性能
To learn how to optimize inserts for large operations, see Inserts Best Practices.要了解如何优化大型操作的插入,请参阅插入最佳实践。
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 Query Best Practices.有关时间序列查询的更多信息,请参阅查询最佳实践。
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
}