$toBool

适用对象: MongoDB vCore

运算符 $toBool 将表达式转换为布尔值。 布尔值按原样返回,而不进行转换。 非零数值会转换为 true,而值为 0 的 Decimal、Long、Double 或 Int 则会转换为 false。 所有其他数据类型都转换为 true。

语法

运算符的 $toBool 语法为:

{ "$toBool": <expression> }

参数

参数 DESCRIPTION
expression 要转换为布尔值的指定值

例子

请查看 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:将 Double 值转换为布尔值

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

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "originalLatitude": 72.8377,
    "latitudeAsBool": true
}

示例 2:将字符串值转换为布尔值

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "originalId": "$_id",
        "idAsBool": {
            "$toBool": "$_id"
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "originalId": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "idAsBool": true
}

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

db.stores.aggregate([
{
    "$match": {
        "_id": "b0107631-9370-4acd-aafa-8ac3511e623d"
    }
},
{
    "$project": {
        "originalTotalSales": "$sales.totalSales",
        "totalSalesAsBool": {
            "$toBool": "$sales.totalSales"
        }
    }
}])

此查询返回以下结果:

{
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "originalTotalSales": 9366,
    "totalSalesAsBool": true
}

此表根据输入值的数据类型来描述$toBool运算符的预期行为。

值类型 行为/输出
布尔值 true 输出 -> true
布尔值假 输出 -> false
任何 “Double”、“Int”、“Long” 或 “Decimal” 值 输出 -> true
任何 ISODate 值 输出 -> true
空值 输出 -> null