$expMovingAvg (aggregation)
On this page本页内容
Definition释义
New in version 5.0. 5.0版新增。
$expMovingAvg
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 | integerthat specifies the number of historical documents that have a significant mathematical weight in the exponential moving average calculation, with the most recent documents contributing the most weight.integer,用于指定在指数移动平均计算中具有重要数学权重的历史文档数,其中最新文档的权重最大。Nvalue is used in this formula to calculate the current result based on the expression value from the current document being read and the previous result of the calculation:N值用于根据当前读取文档的表达式值和之前的计算结果来计算当前结果:current result = current value * ( 2 / ( N + 1 ) ) + | 
| alpha | doublethat specifies the exponential decay value to use in the exponential moving average calculation.double。alphavalue assigns a lower mathematical significance to previous results from the calculation.alpha值越高,计算的先前结果的数学意义越低。alphavalue is used in this formula to calculate the current result based on the expression value from the current document being read and the previous result of the calculation:alpha值在该公式中用于基于正在读取的当前文档的表达式值和先前的计算结果来计算当前结果:current result = current value * alpha + | 
Behavior行为
You must specify either N or alpha. 必须指定N或alpha。You cannot specify both.不能同时指定两者。
$expMovingAvg ignores non-numeric values, 忽略非数值、null values, and missing fields.null值和缺少的字段。
Examples实例
Create a 创建一个stockPrices collection that contains prices for stocks named "MDB" and "MSFT":stockPrices集合,其中包含名为"MDB"和"MSFT"的股票价格:
db.stockPrices.insertMany( [
{ stock: "MDB", date: new Date( "2020-05-18T20:00:00Z" ), price: 13 },
{ stock: "MDB", date: new Date( "2020-05-19T20:00:00Z" ), price: 15.4 },
{ stock: "MDB", date: new Date( "2020-05-20T20:00:00Z" ), price: 12 },
{ stock: "MDB", date: new Date( "2020-05-21T20:00:00Z" ), price: 11.7 },
{ stock: "MSFT", date: new Date( "2020-05-18T20:00:00Z" ), price: 82 },
{ stock: "MSFT", date: new Date( "2020-05-19T20:00:00Z" ), price: 94 },
{ stock: "MSFT", date: new Date( "2020-05-20T20:00:00Z" ), price: 112 },
{ stock: "MSFT", date: new Date( "2020-05-21T20:00:00Z" ), price: 97.3 }
] )
Exponential Moving Average Using N使用N的指数移动平均
NThis 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"- partitions the documents in the collection by按- stock.- stock对集合中的文档进行分区。- There are partitions for有- "MDB"and- "MSFT".- "MDB"和- "MSFT"的分区。
- sortBy: { date: 1 }- sorts the documents in each partition by按- datein ascending order (- 1), so the earliest- dateis first.- date升序(- 1)对每个分区中的文档进行排序,因此最早的- date是第一个。
- output- returns the exponential moving average for the stock返回股票- pricefield with N set to- 2:- price字段的指数移动平均值,其中- N设置为- 2:- In the input documents, there is one document for each day and the documents are ordered by在输入文档中,每天有一个文档,文档按- date.- date排序。- Therefore, with N is set to因此,在- 2, the- pricein the current document and the- pricein the previous document, if available, are allocated the highest weight in the exponential moving average formula.- N设置为- 2的情况下,当前文档中的- price和上一文档中的- price(如果可用)在指数移动平均公式中被分配最高权重。
- The exponential moving average for the- pricefield is stored in a new field called- expMovingAvgForStocks, as shown in the following results.- price字段的指数移动平均值存储在一个名为- expMovingAvgForStocks的新字段中,如以下结果所示。
 
{ "_id" : ObjectId("60d11fef833dfeadc8e6286b"), "stock" : "MDB",
  "date" : ISODate("2020-05-18T20:00:00Z"), "price" : 13,
  "expMovingAvgForStock" : 13 }
{ "_id" : ObjectId("60d11fef833dfeadc8e6286c"), "stock" : "MDB",
  "date" : ISODate("2020-05-19T20:00:00Z"), "price" : 15.4,
  "expMovingAvgForStock" : 14.6 }
{ "_id" : ObjectId("60d11fef833dfeadc8e6286d"), "stock" : "MDB",
  "date" : ISODate("2020-05-20T20:00:00Z"), "price" : 12,
  "expMovingAvgForStock" : 12.866666666666667 }
{ "_id" : ObjectId("60d11fef833dfeadc8e6286e"), "stock" : "MDB",
  "date" : ISODate("2020-05-21T20:00:00Z"), "price" : 11.7,
  "expMovingAvgForStock" : 12.088888888888889 }
{ "_id" : ObjectId("60d11fef833dfeadc8e6286f"), "stock" : "MSFT",
  "date" : ISODate("2020-05-18T20:00:00Z"), "price" : 82,
  "expMovingAvgForStock" : 82 }
{ "_id" : ObjectId("60d11fef833dfeadc8e62870"), "stock" : "MSFT",
  "date" : ISODate("2020-05-19T20:00:00Z"), "price" : 94,
  "expMovingAvgForStock" : 90 }
{ "_id" : ObjectId("60d11fef833dfeadc8e62871"), "stock" : "MSFT",
  "date" : ISODate("2020-05-20T20:00:00Z"), "price" : 112,
  "expMovingAvgForStock" : 104.66666666666667 }
{ "_id" : ObjectId("60d11fef833dfeadc8e62872"), "stock" : "MSFT",
  "date" : ISODate("2020-05-21T20:00:00Z"), "price" : 97.3,
  "expMovingAvgForStock" : 99.75555555555556 }
Exponential Moving Average Using alpha使用alpha的指数移动平均
alphaThis 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"- partitions the documents in the collection by按- stock.- stock对集合中的文档进行分区。- There are partitions for有- "MDB"and- "MSFT".- "MDB"和- "MSFT"的分区。
- sortBy: { date: 1 }- sorts the documents in each partition by按- datein ascending order (- 1), so the earliest- dateis first.- date升序(- 1)对每个分区中的文档进行排序,因此最早的- date是第一个。
- output- sets the exponential moving average for the stock prices in a new field called在名为- expMovingAvgForStock, as shown in the following results.- expMovingAvgForStock的新字段中设置股票价格的指数移动平均值,如以下结果所示。- The value for alpha is set to在指数移动平均公式中,- 0.75in the exponential moving average formula.- alpha的值设置为- 0.75。
{ "_id" : ObjectId("60d11fef833dfeadc8e6286b"), "stock" : "MDB",
  "date" : ISODate("2020-05-18T20:00:00Z"), "price" : 13,
  "expMovingAvgForStock" : 13 }
{ "_id" : ObjectId("60d11fef833dfeadc8e6286c"), "stock" : "MDB",
  "date" : ISODate("2020-05-19T20:00:00Z"), "price" : 15.4,
  "expMovingAvgForStock" : 14.8 }
{ "_id" : ObjectId("60d11fef833dfeadc8e6286d"), "stock" : "MDB",
  "date" : ISODate("2020-05-20T20:00:00Z"), "price" : 12,
  "expMovingAvgForStock" : 12.7 }
{ "_id" : ObjectId("60d11fef833dfeadc8e6286e"), "stock" : "MDB",
  "date" : ISODate("2020-05-21T20:00:00Z"), "price" : 11.7,
  "expMovingAvgForStock" : 11.95 }
{ "_id" : ObjectId("60d11fef833dfeadc8e6286f"), "stock" : "MSFT",
  "date" : ISODate("2020-05-18T20:00:00Z"), "price" : 82,
  "expMovingAvgForStock" : 82 }
{ "_id" : ObjectId("60d11fef833dfeadc8e62870"), "stock" : "MSFT",
  "date" : ISODate("2020-05-19T20:00:00Z"), "price" : 94,
  "expMovingAvgForStock" : 91 }
{ "_id" : ObjectId("60d11fef833dfeadc8e62871"), "stock" : "MSFT",
  "date" : ISODate("2020-05-20T20:00:00Z"), "price" : 112,
  "expMovingAvgForStock" : 106.75 }
{ "_id" : ObjectId("60d11fef833dfeadc8e62872"), "stock" : "MSFT",
  "date" : ISODate("2020-05-21T20:00:00Z"), "price" : 97.3,
  "expMovingAvgForStock" : 99.6625 }