$range

$range 运算符用于生成顺序整数数组。 运算符有助于在区域中创建数字数组,这对于分页、索引或测试数据非常有用。

语法

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

参数

参数 说明
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
        ]
    }
]

- 查看用于 从 MongoDB 迁移到 Azure Cosmos DB for MongoDB(vCore)的选项。 - 详细了解 与 MongoDB 的功能兼容性