$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 形式に/から変換する際の入力または出力の形式
onError (省略可能)指定した型への入力の変換が失敗したときに返す値
onNull (省略可能)指定した型に変換する入力値が null または不足している場合に返す値

例示

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 値を文字列に変換する

fullTime フィールドを整数から文字列に変換するには、$convert演算子を使用してクエリを実行して変換します。

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 値をブール値に変換する

fullTime フィールドを整数からブール値に変換するには、$convert演算子を使用してクエリを実行して変換します。 fullTime フィールドの正の値は true に変換されます。

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 値を Decimal 値に変換する

fullTime スタッフ フィールドを整数から 10 進値に変換するには、$convert演算子を使用してクエリを実行して変換を行います。

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')"
    }
]