On this page本页内容
New in version 5.0.在版本5.0中新增。
Returns the exponential moving average of numeric expressions applied to documents in a partition defined in the 返回应用于$setWindowFields
stage.$setWindowFields
阶段中定义的分区中的文档的数值表达式的指数移动平均值。
$expMovingAvg
is only available in the 仅在$setWindowFields
stage.$setWindowFields
阶段可用。
$expMovingAvg
syntax:语法:
{ $expMovingAvg: { input: <input expression>, N: <integer>, alpha: <float> } }
$expMovingAvg
takes a document with these fields:获取包含以下字段的文档:
input |
|
N |
current result = current value * ( 2 / ( N + 1 ) ) + previous result * ( 1 - ( 2 / ( N + 1 ) ) ) |
alpha |
current result = current value * alpha + previous result * ( 1 - alpha ) |
You must specify either N or alpha. 必须指定N或alpha。You cannot specify both.不能同时指定两者。
$expMovingAvg
ignores non-numeric values, 忽略非数值、null
values, and missing fields.null
值和缺少的字段。
Create a 创建包含名为stockPrices
collection that contains prices for stocks named "ABC"
and "DEF"
:"ABC"
和"DEF"
的股票价格的stockPrices
集合:
db.stockPrices.insertMany( [ { stock: "ABC", date: new Date( "2020-05-18T20:00:00Z" ), price: 13 }, { stock: "ABC", date: new Date( "2020-05-19T20:00:00Z" ), price: 15.4 }, { stock: "ABC", date: new Date( "2020-05-20T20:00:00Z" ), price: 12 }, { stock: "ABC", date: new Date( "2020-05-21T20:00:00Z" ), price: 11.7 }, { stock: "DEF", date: new Date( "2020-05-18T20:00:00Z" ), price: 82 }, { stock: "DEF", date: new Date( "2020-05-19T20:00:00Z" ), price: 94 }, { stock: "DEF", date: new Date( "2020-05-20T20:00:00Z" ), price: 112 }, { stock: "DEF", date: new Date( "2020-05-21T20:00:00Z" ), price: 97.3 } ] )
N
N
的指数移动平均This example uses 本例使用$expMovingAvg
in the $setWindowFields
stage to output the exponential moving average for the stock prices weighted for two historical documents (two days for the example documents) using N set to 2
:$setWindowFields
阶段中的$expMovingAvg
输出两个历史文档(示例文档为两天)加权的股票价格的指数移动平均值,使用N
设置为2
:
db.stockPrices.aggregate( [ { $setWindowFields: { partitionBy: "$stock", sortBy: { date: 1 }, output: { expMovingAvgForStock: { $expMovingAvg: { input: "$price", N: 2 } } } } } ] )
In the example:在该示例中:
partitionBy: "$stock"
stock
. stock
对集合中的文档进行分区。"ABC"
and "DEF"
."ABC"
和"DEF"
的分区。sortBy: { date: 1 }
date
in ascending order (1
), so the earliest date
is first.date
按升序(1
)对每个分区中的文档进行排序,因此最早的date
是第一个。output
returns the exponential moving average for the stock 返回股票price
field with N set to 2
:price
字段的指数移动平均值,N
设置为2
:
date
. date
排序。2
, the price
in the current document and the price
in the previous document, if available, are allocated the highest weight in the exponential moving average formula.N
设置为2
时,当前文档中的price
和上一文档中的price
(如果可用)将在指数移动平均公式中分配最高权重。price
field is stored in a new field called expMovingAvgForStocks
, as shown in the following results.price
字段的指数移动平均值存储在名为expMovingAvgForStocks
的新字段中,如以下结果所示。{ "_id" : ObjectId("60d11fef833dfeadc8e6286b"), "stock" : "ABC", "date" : ISODate("2020-05-18T20:00:00Z"), "price" : 13, "expMovingAvgForStock" : 13 } { "_id" : ObjectId("60d11fef833dfeadc8e6286c"), "stock" : "ABC", "date" : ISODate("2020-05-19T20:00:00Z"), "price" : 15.4, "expMovingAvgForStock" : 14.6 } { "_id" : ObjectId("60d11fef833dfeadc8e6286d"), "stock" : "ABC", "date" : ISODate("2020-05-20T20:00:00Z"), "price" : 12, "expMovingAvgForStock" : 12.866666666666667 } { "_id" : ObjectId("60d11fef833dfeadc8e6286e"), "stock" : "ABC", "date" : ISODate("2020-05-21T20:00:00Z"), "price" : 11.7, "expMovingAvgForStock" : 12.088888888888889 } { "_id" : ObjectId("60d11fef833dfeadc8e6286f"), "stock" : "DEF", "date" : ISODate("2020-05-18T20:00:00Z"), "price" : 82, "expMovingAvgForStock" : 82 } { "_id" : ObjectId("60d11fef833dfeadc8e62870"), "stock" : "DEF", "date" : ISODate("2020-05-19T20:00:00Z"), "price" : 94, "expMovingAvgForStock" : 90 } { "_id" : ObjectId("60d11fef833dfeadc8e62871"), "stock" : "DEF", "date" : ISODate("2020-05-20T20:00:00Z"), "price" : 112, "expMovingAvgForStock" : 104.66666666666667 } { "_id" : ObjectId("60d11fef833dfeadc8e62872"), "stock" : "DEF", "date" : ISODate("2020-05-21T20:00:00Z"), "price" : 97.3, "expMovingAvgForStock" : 99.75555555555556 }
alpha
alpha
的指数移动平均This example uses 本例使用$expMovingAvg
in the $setWindowFields
stage to output the exponential moving average for the stock prices using alpha set to 0.75
:$setWindowFields
阶段中的$expMovingAvg
,使用alpha
设置为0.75
输出股票价格的指数移动平均值:
db.stockPrices.aggregate( [ { $setWindowFields: { partitionBy: "$stock", sortBy: { date: 1 }, output: { expMovingAvgForStock: { $expMovingAvg: { input: "$price", alpha: 0.75 } } } } } ] )
In the example:在该示例中:
partitionBy: "$stock"
stock
. stock
对集合中的文档进行分区。"ABC"
and "DEF"
."ABC"
和"DEF"
的分区。sortBy: { date: 1 }
date
in ascending order (1
), so the earliest date
is first.date
按升序(1
)对每个分区中的文档进行排序,因此最早的date
是第一个。output
expMovingAvgForStock
, as shown in the following results. expMovingAvgForStock
的新字段中设置股票价格的指数移动平均值,如以下结果所示。0.75
in the exponential moving average formula.alpha
值设置为0.75
。{ "_id" : ObjectId("60d11fef833dfeadc8e6286b"), "stock" : "ABC", "date" : ISODate("2020-05-18T20:00:00Z"), "price" : 13, "expMovingAvgForStock" : 13 } { "_id" : ObjectId("60d11fef833dfeadc8e6286c"), "stock" : "ABC", "date" : ISODate("2020-05-19T20:00:00Z"), "price" : 15.4, "expMovingAvgForStock" : 14.8 } { "_id" : ObjectId("60d11fef833dfeadc8e6286d"), "stock" : "ABC", "date" : ISODate("2020-05-20T20:00:00Z"), "price" : 12, "expMovingAvgForStock" : 12.7 } { "_id" : ObjectId("60d11fef833dfeadc8e6286e"), "stock" : "ABC", "date" : ISODate("2020-05-21T20:00:00Z"), "price" : 11.7, "expMovingAvgForStock" : 11.95 } { "_id" : ObjectId("60d11fef833dfeadc8e6286f"), "stock" : "DEF", "date" : ISODate("2020-05-18T20:00:00Z"), "price" : 82, "expMovingAvgForStock" : 82 } { "_id" : ObjectId("60d11fef833dfeadc8e62870"), "stock" : "DEF", "date" : ISODate("2020-05-19T20:00:00Z"), "price" : 94, "expMovingAvgForStock" : 91 } { "_id" : ObjectId("60d11fef833dfeadc8e62871"), "stock" : "DEF", "date" : ISODate("2020-05-20T20:00:00Z"), "price" : 112, "expMovingAvgForStock" : 106.75 } { "_id" : ObjectId("60d11fef833dfeadc8e62872"), "stock" : "DEF", "date" : ISODate("2020-05-21T20:00:00Z"), "price" : 97.3, "expMovingAvgForStock" : 99.6625 }