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.前面的管道将创建或更新dailytemperatureaverages集合,其中包含基于weather集合的所有每日平均温度。

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.有关物化视图的详细信息,请参阅按需物化视图

←  Migrate Data into a Time Series CollectionShard a Time Series Collection →