$toBool演算子は、式をブール値に変換します。 ブール値は、変換なしでそのまま返されます。 0 以外の数値は true に変換され、0 の Decimal、Long、Double、または Int の値は false に変換されます。 その他のすべてのデータ型は true に変換されます。
構文
{
$toBool: < expression >
}
パラメーター
| パラメーター | Description |
|---|---|
expression |
ブール値に変換する指定された値 |
例示
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 値をブール値に変換する
緯度フィールドの値を double からブール値に変換するには、フィールドの $toBool 演算子を使用してクエリを実行し、変換を行います。 正の数値は true に変換されます。
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: 文字列値をブール値に変換する
_id フィールドの値を文字列からブール値に変換するには、$toBool値を使用してクエリを実行して変換します。 文字列値は true に変換されます。
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 値をブール値に変換する
sales.totalSales フィールドの値を整数からブール値に変換するには、$toBool演算子を使用してクエリを実行して変換します。 正の数値は true に変換されます。
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演算子の予期される動作を示しています。
| バリュータイプ | Behavior/Output |
|---|---|
| ブール値 真 | 出力 -> true |
| ブール値 false | 出力 -> false |
| Double、Int、Long、または Decimal のいずれかの値 | 出力 -> true |
| 任意の ISODate 値 | 出力 -> true |
| Null 値 | 出力 -> null |