Some aggregation stages and operators require special considerations when you use them with time series collections.当您将某些聚合阶段和运算符与时间序列集合一起使用时,需要特别注意。
$geonear
Time series collections only support the 时间序列集合仅支持$geoNear aggregation stage for sorting geospatial data from queries against 2dsphere indexes. $geoNear聚合阶段,用于对2dsphere索引查询中的地理空间数据进行排序。You can't use the 您不能在时间序列集合上使用$near and $nearSphere operators on time series collections.$near和$nearSphere运算符。
$merge
You cannot use the 您不能使用$merge aggregation stage to add data from another collection to a time series collection.$merge聚合阶段将其他集合中的数据添加到时间序列集合中。
$out
Starting in MongoDB 7.0, you can use the 从MongoDB 7.0开始,您可以使用$out aggregation stage to write documents to a time series collection. $out聚合阶段将文档写入时间序列集合。For more information, see Migrate Data into a Time Series Collection.有关详细信息,请参阅将数据迁移到时间序列集合中。
Frequently Used Operations常用操作
The following aggregation pipeline operators and stages are often used to analyze time series data:以下聚合管道运算符和阶段通常用于分析时间序列数据:
$dateAdd: Adds a specified amount of time to a Date object.:向Date对象添加指定的时间量。$dateDiff: Returns the time difference between two dates.:返回两个日期之间的时间差。$dateTrunc: Returns a date that has been truncated to the specific unit.:返回已截断为特定单位的日期。$setWindowFields: Runs calculations on documents in a given window.:对给定窗口中的文档运行计算。
Examples示例
Calculate Average Price per Month计算每月平均价格
Consider a 考虑一个dowJonesTickerData collection that contains documents with the following structure:dowJonesTickerData集合,其中包含具有以下结构的文档:
{
date: ISODate("2020-01-03T05:00:00.000Z"),
symbol: 'AAPL',
volume: 146322800,
open: 74.287498,
adjClose: 73.486023,
high: 75.144997,
low: 74.125,
close: 74.357498
}
This aggregation pipeline performs the following actions:此聚合管道执行以下操作:
Uses使用$dateTruncto truncate each document'sdateto the appropriate month.$dateTrunc将每个文档的date截断到相应的月份。Uses使用$groupto group the documents by month and symbol.$group按月份和符号对文档进行分组。Uses使用$avgto calculate the average price per month.$avg计算每月的平均价格。
db.dowJonesTickerData.aggregate( [ {
$group: {
_id: {
firstDayOfMonth: {
$dateTrunc: {
date: "$date",
unit: "month"
}
},
symbol: "$symbol"
},
avgMonthClose: {
$avg: "$close"
}
}
} ] )
The pipeline returns a set of documents where each document contains the average closing price per month for a particular stock.管道返回一组文档,其中每个文档都包含特定股票的每月平均收盘价。
{
_id: {
firstDayOfMonth: ISODate("2020-06-01T00:00:00.000Z"),
symbol: 'GOOG'
},
avgMonthClose: 1431.0477184545455
},
{
_id: {
firstDayOfMonth: ISODate("2021-07-01T00:00:00.000Z"),
symbol: 'MDB'
},
avgMonthClose: 352.7314293333333
},
{
_id: {
firstDayOfMonth: ISODate("2021-06-01T00:00:00.000Z"),
symbol: 'MSFT'
},
avgMonthClose: 259.01818086363636
}Calculate a Rolling Average Over 30 Days计算30天内的滚动平均值
Consider a 考虑一个dowJonesTickerData collection that contains documents with the following structure:dowJonesTickerData集合,其中包含具有以下结构的文档:
{
date: ISODate("2020-01-03T05:00:00.000Z"),
symbol: 'AAPL',
volume: 146322800,
open: 74.287498,
adjClose: 73.486023,
high: 75.144997,
low: 74.125,
close: 74.357498
}
This aggregation pipeline performs the following operations:此聚合管道执行以下操作:
Uses使用$setWindowFieldsto specify a window of 30 days.$setWindowFields指定30天的窗口。Calculates a rolling average of the closing price over the last 30 days for each stock.计算每只股票过去30天收盘价的滚动平均值。
db.dowJonesTickerData.aggregate( [
{ $setWindowFields: {
partitionBy: { symbol : "$symbol" } ,
sortBy: { date: 1 },
output: {
averageMonthClosingPrice: {
$avg : "$close",
window : { range : [-1, "current"], unit : "month" }
}
}
} }
] )
The pipeline returns a set of documents where each document includes a 管道返回一组文档,其中每个文档都包含一个$averageMonthClosingPrice field that contains the average of the previous month's closing price for that stock symbol.$averageMonthClosingPrice字段,该字段包含该股票代码上个月收盘价的平均值。
{
date: ISODate("2020-01-29T05:00:00.000Z"),
symbol: 'AAPL',
volume: 216229200,
adjClose: 80.014801,
low: 80.345001,
high: 81.962502,
open: 81.112503,
close: 81.084999,
averageMonthClosingPrice: 77.63137520000001
}