$round

$round 運算子用來將數字四捨五入至指定的小數位。 在數值精確度很重要的匯總中這很有用,例如財務計算或統計分析。

語法

{
  $round: [ <number>, <place> ]
}

參數

參數 Description
<number> 要四捨五入的數字。
<place> 數字應四捨五入的小數位數。

範例

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

{
    "_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 - 將商店的位置座標四捨五入

若要將「第一顧問」公司內所有商店的緯度和經度四捨五入,請先執行查詢以篩選公司的名稱。 然後,使用 lat 和 lon 字段上的 $round 運算符,傳回所需的結果。

db.stores.aggregate([{
    $match: {
        company: {
            $in: ["First Up Consultants"]
        }
    }
}, {
    $project: {
        company: 1,
        "location.lat": 1,
        "location.lon": 1,
        roundedLat: {
            $round: ["$location.lat", 1]
        },
        roundedLon: {
            $round: ["$location.lon", 1]
        }
    }
}])

此查詢傳回的前三個結果為:

[
    {
        "_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
        "location": {
            "lat": 87.2239,
            "lon": -129.0506
        },
        "company": "First Up Consultants",
        "roundedLat": 87.2,
        "roundedLon": -129.1
    },
    {
        "_id": "26afb024-53c7-4e94-988c-5eede72277d5",
        "location": {
            "lat": -29.1866,
            "lon": -112.7858
        },
        "company": "First Up Consultants",
        "roundedLat": -29.2,
        "roundedLon": -112.8
    },
    {
        "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad",
        "location": {
            "lat": -0.2136,
            "lon": 108.7466
        },
        "company": "First Up Consultants",
        "roundedLat": -0.2,
        "roundedLon": 108.7
    }
]

範例 2 - 四捨五入到最接近的千位

若要將「第一向上顧問」公司內商店的總銷售量四捨五入,請首先執行查詢,按公司名稱篩選商店。 然後使用 totalSales 字段上的 $round 運算符,將值四捨五入到最接近的千。

db.stores.aggregate([{
    $match: {
        company: {
            $in: ["First Up Consultants"]
        }
    }
}, {
    $project: {
        company: 1,
        "sales.totalSales": 1,
        roundedSales: {
            $round: ["$sales.totalSales", -3]
        }
    }
}])

此查詢傳回的前三個結果為:

[
    {
        "_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
        "sales": {},
        "company": "First Up Consultants",
        "roundedSales": 279000
    },
    {
        "_id": "26afb024-53c7-4e94-988c-5eede72277d5",
        "sales": {},
        "company": "First Up Consultants",
        "roundedSales": 50000
    },
    {
        "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad",
        "sales": {},
        "company": "First Up Consultants",
        "roundedSales": 69000
    }
]