$expMovingAvg

$expMovingAvg演算子は、指定されたフィールドの値の指数移動平均を計算します。

構文

{
    $expMovingAvg: {
        input: < field to use for calculation >,
        N: < number of recent documents with the highest weight
    }
}

パラメーター

パラメーター Description
input 指数移動平均の計算に使用される値を持つフィールド
N 指数移動平均の計算で最も重みが高い前のドキュメントの数

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

例 1 - 売上合計の指数移動平均を計算する

First Up Consultants 企業内のすべての店舗の売上合計の指数移動平均を取得するには、最初にクエリを実行して会社をフィルター処理します。 次に、結果のドキュメントを開く日付の昇順で並べ替えます。 最後に、2 つの最新のドキュメントに最も高い重みを割り当てて、総売上の指数移動平均を計算しました。

db.stores.aggregate(
[{
      "$match": {
          "company": {
              "$in": [
                  "First Up Consultants"
              ]
          }
      }
  },
  {
    "$setWindowFields": {
        "partitionBy": "$company",
        "sortBy": {
            "storeOpeningDate": 1
        },
        "output": {
            "expMovingAvgForSales": {
                "$expMovingAvg": {
                    "input": "$sales.totalSales",
						        "N": 2
                 }
              }
          }
      }
  },
  {
    "$project": {
        "company": 1,
        "name": 1,
        "sales.totalSales": 1,
        "storeOpeningDate": 1,
        "expMovingAvgForSales": 1
    }
  }])

このクエリによって返される 2 つの結果は次のとおりです。

[
    {
        "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
        "sales": {
            "revenue": 37701
        },
        "company": "First Up Consultants",
        "storeOpeningDate": {
            "$date": 1633219200000
        },
        "name": "First Up Consultants | Bed and Bath Center - South Amir",
        "expMovingAvgForSales": 37701
    },
    {
        "_id": "8e7a259b-f7d6-4ec5-a521-3bed53adc587",
        "name": "First Up Consultants | Drone Stop - Lake Joana",
        "sales": {
            "revenue": 14329
        },
        "company": "First Up Consultants",
        "storeOpeningDate": {
            "$date": 1706958339311
        },
        "expMovingAvgForSales": 22119.666666666668
    }
]

例 2 - アルファ パラメーターを使用して売上合計の指数移動平均を計算する

First Up Consultants 企業内のすべての店舗の売上合計の指数移動平均を取得するには、最初にクエリを実行して会社をフィルター処理します。 次に、結果のドキュメントを開く日付の昇順で並べ替えます。 最後に、減衰率 (アルファ) を指定して、総売上の指数移動平均を計算します。 アルファ値を大きくすると、前のドキュメントの計算の重みが小さくなります。

db.stores.aggregate(
[{
        "$match": {
            "company": {
                "$in": [
                    "First Up Consultants"
                ]
            }
        }
    },
    {
        "$setWindowFields": {
            "partitionBy": "$company",
            "sortBy": {
                "storeOpeningDate": 1
            },
            "output": {
                "expMovingAvgForSales": {
                    "$expMovingAvg": {
                        "input": "$sales.totalSales",
                        "alpha": 0.75
                    }
                }
            }
        }
    },
    {
        "$project": {
            "company": 1,
            "name": 1,
            "sales.totalSales": 1,
            "storeOpeningDate": 1,
            "expMovingAvgForSales": 1
        }
    }
])

このクエリによって返される最初の 2 つの結果は次のとおりです。

[
    {
        "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
        "sales": {
            "revenue": 37701
        },
        "company": "First Up Consultants",
        "storeOpeningDate": "2021-10-03T00:00:00.000Z",
        "name": "First Up Consultants | Bed and Bath Center - South Amir",
        "expMovingAvgForSales": 37701
    },
    {
        "_id": "8e7a259b-f7d6-4ec5-a521-3bed53adc587",
        "name": "First Up Consultants | Drone Stop - Lake Joana",
        "sales": {
            "revenue": 14329
        },
        "company": "First Up Consultants",
        "storeOpeningDate": "2024-09-02T00:05:39.311Z",
        "expMovingAvgForSales": 20172
    }
]