정의
버전 5.0의 신규 항목입니다.
$setWindowFields 단계에서 정의된 파티션의 문서에 적용된 숫자 표현식의 지수 이동 평균을 반환합니다.
$expMovingAvg is only available in the $setWindowFields stage.
$expMovingAvg 구문:
{ $expMovingAvg: { input: <input expression>, N: <integer>, alpha: <float> } }
$expMovingAvg (은)는 다음 필드가 있는 문서를 가져옵니다.
필드 | 설명 |
|---|---|
평가할 표현식을 지정합니다. 숫자가 아닌 표현식은 무시됩니다. | |
행동
반드시 N 또는 alpha를 지정해야 합니다. 둘 다 지정할 수는 없습니다.
$expMovingAvg ignores non-numeric values, null values, and missing fields.
예시
"MDB" 및 "MSFT" 주식의 가격이 포함된 stockPrices 컬렉션을 만듭니다.
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 } ] )
다음을 사용한 지수 이동 평균 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:
db.stockPrices.aggregate( [ { $setWindowFields: { partitionBy: "$stock", sortBy: { date: 1 }, output: { expMovingAvgForStock: { $expMovingAvg: { input: "$price", N: 2 } } } } } ] )
예시:
partitionBy: "$stock"는 collection의 문서를stock로 파티셔닝합니다."MDB"및"MSFT"에 대한 파티션이 있습니다.sortBy: { date: 1 }각 파티션의 문서를date을 기준으로 오름차순(1)으로 정렬하므로, 가장 이른date이 첫 번째가 됩니다.outputreturns the exponential moving average for the stockpricefield with N set to2:In the input documents, there is one document for each day and the documents are ordered by
date. Therefore, with N is set to2, thepricein the current document and thepricein the previous document, if available, are allocated the highest weight in the exponential moving average formula.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 }
다음을 사용한 지수 이동 평균 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:
db.stockPrices.aggregate( [ { $setWindowFields: { partitionBy: "$stock", sortBy: { date: 1 }, output: { expMovingAvgForStock: { $expMovingAvg: { input: "$price", alpha: 0.75 } } } } } ] )
예시:
partitionBy: "$stock"는 collection의 문서를stock로 파티셔닝합니다."MDB"및"MSFT"에 대한 파티션이 있습니다.sortBy: { date: 1 }각 파티션의 문서를date을 기준으로 오름차순(1)으로 정렬하므로, 가장 이른date이 첫 번째가 됩니다.outputsets the exponential moving average for the stock prices in a new field calledexpMovingAvgForStock, as shown in the following results. The value for alpha is set to0.75in the exponential moving average formula.
{ "_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 }