Database Manual / Time Series / Query

Build Materialized Views on Top of Time Series Data在时间序列数据之上构建物化视图

Materialized views on time series data are useful for:时间序列数据的物化视图有助于:

To create an On-Demand Materialized view, use the $merge aggregation pipeline stage to transform and store your data:要创建按需物化视图,请使用$merge聚合管道阶段转换和存储数据:

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" }
}
}, {
$merge: { into: "dailytemperatureaverages", whenMatched: "replace" }
}
])

The preceding pipeline, will create or update the dailytemperatureaverages collection with all daily temperature averages based on the weather collection.前面的管道将创建或更新基于weather集合的所有每日温度平均值的dailytemperatureaverages集合。

Note

It is not possible to natively schedule the refreshing of these materialized views.无法以本机方式安排这些物化视图的刷新。

For more information on materialized views, see On-Demand Materialized Views.有关物化视图的更多信息,请参阅按需物化视图