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.dailytemperatureaverages集合,其中包含基于weather集合的所有每日平均温度。
It is not possible to natively schedule the refreshing of these materialized views.无法以本机方式安排这些物化视图的刷新。
For more information on materialized views, see On-Demand Materialized Views.有关物化视图的详细信息,请参阅按需物化视图。