$range

$range運算子是用來產生循序整數數位。 運算子有助於在範圍中建立數字數位數位數位,對分頁、編制索引或測試數據很有用。

語法

{
    $range: [ <start>, <end>, <step> ]
}

參數

參數 Description
start 範圍的起始值(含)。
end 範圍的結束值(獨佔)。
step 範圍中每個數字之間的遞增值(選擇性,預設值為 1)。

範例

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

{
    "_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:產生數位範圍

此查詢示範如何使用運算子產生 0 到 5 的整數陣列,其中它包含左邊界,同時排除右邊界。

db.stores.aggregate([{
    $match: {
        _id: "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6"
    }
}, {
    $project: {
        rangeArray: {
            $range: [0, 5]
        }
    }
}])

此查詢會傳回下列結果。

[
    {
        "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
        "rangeArray": [
            0,
            1,
            2,
            3,
            4
        ]
    }
]

範例 2:產生具有步驟值的數字範圍

此查詢示範如何使用運算子來產生 0 到 18 的偶數陣列。

db.stores.aggregate([{
    $match: {
        _id: "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6"
    }
}, {
    $project: {
        evenNumbers: {
            $range: [0, 8, 2]
        }
    }
}])

此查詢會產生下列結果。

[
    {
        "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
        "rangeArray": [
            0,
            2,
            4,
            6
        ]
    }
]