Materialized views on time series data are useful for:时间序列数据的物化视图有助于:
archiving归档analytics分析facilitating data access for teams that cannot access the raw data为无法访问原始数据的团队提供数据访问便利
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.有关物化视图的更多信息,请参阅按需物化视图。