$convert

适用对象: MongoDB vCore

$convert运算符将表达式转换为指定类型的值。 如果输入表达式转换为指定类型失败,$convert运算符也会执行指定的作。

语法

运算符的 $convert 语法为:

{ "$convert": {"input": <expression>, "to": <type>, "format": <binData format>, "onError": <value to return on error>, "onNull": <value to return on null> }

参数

参数 DESCRIPTION
input 要转换为指定类型的输入值
to 要将输入值转换为的类型
format (可选)在将值转换为 binData 或从 binData 转换时,输入或输出的 binData 格式
onError (可选)将输入转换为指定类型失败时要返回的值
onNull (可选)当要转换为指定类型的输入值为 null 或缺失时要返回的值

例子

请查看 StoreData 数据库中 stores 集合的这个示例文档。

{
    "_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:将 Int 值转换为字符串

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "fulltimeStaff": "$staff.totalStaff.fullTime",
        "fulltimeStaffAsString": {
            "$convert": {
                "input": "$staff.totalStaff.fullTime",
                "to": "string"
            }
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "fulltimeStaff": 3,
    "fulltimeStaffAsString": "3"
}

示例 2:将 Int 值转换为布尔值

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "fulltimeStaff": "$staff.totalStaff.fullTime",
        "fulltimeStaffAsBool": {
            "$convert": {
                "input": "$staff.totalStaff.fullTime",
                "to": "bool"
            }
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "fulltimeStaff": 3,
    "fulltimeStaffAsBool": true
}

示例 3:将 Int 值转换为十进制值

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "fulltimeStaff": "$staff.totalStaff.fullTime",
        "fulltimeStaffAsDecimal": {
            "$convert": {
                "input": "$staff.totalStaff.fullTime",
                "to": "decimal"
            }
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "fulltimeStaff": 3,
    "fulltimeStaffAsDecimal": "Decimal128('3')"
}

示例 4:将 Int 值转换为 Long 值

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "fulltimeStaff": "$staff.totalStaff.fullTime",
        "fulltimeStaffAsLong": {
            "$convert": {
                "input": "$staff.totalStaff.fullTime",
                "to": "long"
            }
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "fulltimeStaff": 3,
    "fulltimeStaffAsLong": "Long('3')"
}