共用方式為


$縮減

$reduce運算子可用來將表達式套用至陣列中的每個專案,並將結果累積成單一值。 此運算子適用於執行需要逐一查看數位元素並匯總其值的作業。

語法

$reduce: {
   input: <array>,
   initialValue: <expression>,
   in: <expression>
}

參數

參數 Description
input 要逐一查看的陣列。
initialValue 陣組反覆項目開始之前設定的初始累計值。
in 評估為陣列中每個項目的累積值的有效運算式。

範例

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

{
    "_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:匯總數組值

此查詢示範如何使用來 $reduce 加總陣列中 salesByCategory 不同類別的總銷售額。

db.stores.aggregate([{
    $match: {
        _id: "988d2dd1-2faa-4072-b420-b91b95cbfd60"
    }
}, {
    $project: {
        totalSalesByCategory: {
            $reduce: {
                input: "$sales.salesByCategory.totalSales",
                initialValue: 0,
                in: {
                    $add: ["$$value", "$$this"]
                }
            }
        }
    }
}])

查詢會傳回下列結果。

[
  {
      "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
      "totalSalesByCategory": 149849
  }
]