$derivative

運算子會 $derivative 對分割區內一或多個欄位上的文件進行排序,並計算視窗內第一個和最後一個文件之間欄位的平均變更率。

語法

{
    $derivative: {
        input: < expression >,
        unit: < timeWindow >
    }
}

參數

參數 Description
input 計算範圍速率的運算式或欄位
unit 變化率的時間範圍

範例

請參考商店集合中的此範例檔。

{
    "_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 公司中每個商店的總銷售額導數,請先執行查詢以篩選公司,以最後更新的時間戳記遞增順序排序產生的文件,並計算結果集中第一個文件與目前文件之間總銷售額的導數 (平均變化率)。

db.stores.aggregate([{
        "$match": {
            "company": {
                "$in": [
                    "First Up Consultants"
                ]
            },
            "$and": [{
                    "lastUpdated": {
                        "$gt": ISODate("2024-12-01T03:06:24.180Z")
                    }
                },
                {
                    "lastUpdated": {
                        "$lt": ISODate("2025-12-01T03:55:17.557Z")
                    }
                }
            ]
        }
    },
    {
        "$setWindowFields": {
            "partitionBy": "$company",
            "sortBy": {
                "lastUpdated": 1
            },
            "output": {
                "storeAverageSales": {
                    "$derivative": {
                        "input": "$sales.totalSales",
                        "unit": "week"
                    },
                    "window": {
                        "range": [
                            -1,
                            0
                        ],
                        "unit": "week"
                    }
                }
            }
        }
    },
    {
        "$project": {
            "lastUpdated": 1,
            "storeAverageSales": 1
        }
    }
])

此查詢會傳回下列結果:

[
    {
        "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
        "lastUpdated": "2025-06-11T10:48:01.291Z",
        "storeAverageSales": 21554495.708753344
    }
]