$toInt

運算子 $toInt 會將指定的值轉換成整數值。

語法

運算子的 $toInt 語法為:

{
    $toInt: < expression >
}

參數

參數 Description
expression 要轉換成 Integer 值的指定值

範例

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

{
    "_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:將 Double 值轉換成 Integer 值

若要將緯度字段的值從 double 型別轉換成 int,請在字段上使用 $toInt 運算元進行查詢操作,以完成轉換。

db.stores.aggregate([{
    $match: {
        _id: "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
}, {
    $project: {
        originalLatitude: "$location.lat",
        latitudeAsInt: {
            $toInt: "$location.lat"
        }
    }
}])

此查詢會傳回下列結果:

[
    {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
        "originalLatitude": 72.8377,
        "latitudeAsInt": 72
    }
]

範例 2:將 String 值轉換成 Integer 值

若要將 72 (“72”) 的字串表示轉換成整數值,請在值上使用 $toInt 運算符執行查詢,以進行轉換。

db.stores.aggregate([{
    $match: {
        _id: "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
}, {
    $project: {
        originalLatitude: "$location.lat",
        latitudeAsInt: {
            $toInt: {
                $toString: "72"
            }
        }
    }
}])

此查詢會傳回下列結果:

[
    {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
        "originalLatitude": 72.8377,
        "latitudeAsInt": 72
    }
]

不過,下列查詢會傳回錯誤,因為字串 「72.0」 不是整數值的字串表示。

db.stores.aggregate([{
    $match: {
        _id: "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
}, {
    $project: {
        originalLatitude: "$location.lat",
        latitudeAsInt: {
            $toInt: {
                $toString: "72.0"
            }
        }
    }
}])

此查詢會傳回下列錯誤訊息 -“無法剖析$convert中的數位 '72.0'

此數據表會根據輸入的數據類型來描述$toInt運算符的預期行為。

實值類型 Behavior/Output
布爾值 真 輸出 -> 1
布爾值 假 輸出 -> 0
Double 值。 例如 72.0 輸出 -> 72
整數值的字串表示。 例如,“72” 輸出 -> 72
Double 值的字串表示。 例如,“72.0” 輸出 -> 錯誤
Null 值 輸出 -> null