Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
$toBool işleci bir ifadeyi Boole değerine dönüştürür. Boole değerleri dönüştürme olmadan olduğu gibi döndürülür. Sıfır olmayan sayısal değerler doğruya dönüştürülürken, Ondalık, Uzun, Çift veya Int türlerinden 0 olanlar yanlışa dönüştürülür. Diğer tüm veri türleri true'ya dönüştürülür.
Sözdizimi
{
$toBool: < expression >
}
Parametreler
| Parametre | Description |
|---|---|
expression |
Boole değerine dönüştürülecek belirtilen değer |
Örnekler
Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.
{
"_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
}
]
}
]
}
Örnek 1: Çift değerini Boole değerine dönüştürme
Enlem alanının değerini bir çiftten boole değerine dönüştürmek için, dönüştürmeyi yapmak için alandaki $toBool işlecini kullanarak bir sorgu çalıştırın. Pozitif sayısal değerler true değerine dönüştürülür.
db.stores.aggregate([{
$match: {
_id: "b0107631-9370-4acd-aafa-8ac3511e623d"
}
}, {
$project: {
originalLatitude: "$location.lat",
latitudeAsBool: {
$toBool: "$location.lat"
}
}
}])
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"originalLatitude": 72.8377,
"latitudeAsBool": true
}
]
Örnek 2: Dize değerini Boole değerine dönüştürme
_id alanının değerini bir dizeden boole değerine dönüştürmek için, dönüştürmeyi yapmak için $toBool değerini kullanarak bir sorgu çalıştırın. Dize değerleri true değerine dönüştürülür.
db.stores.aggregate([{
$match: {
_id: "b0107631-9370-4acd-aafa-8ac3511e623d"
}
}, {
$project: {
originalId: "$_id",
idAsBool: {
$toBool: "$_id"
}
}
}])
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"originalId": "b0107631-9370-4acd-aafa-8ac3511e623d",
"idAsBool": true
}
]
Örnek 3: Int değerini Boole değerine dönüştürme
sales.totalSales alanının değerini tamsayıdan boole değerine dönüştürmek için, dönüştürmeyi yapmak için $toBool işlecini kullanarak bir sorgu çalıştırın. Pozitif sayısal değerler true değerine dönüştürülür.
db.stores.aggregate([{
$match: {
_id: "b0107631-9370-4acd-aafa-8ac3511e623d"
}
}, {
$project: {
originalTotalSales: "$sales.totalSales",
totalSalesAsBool: {
$toBool: "$sales.totalSales"
}
}
}])
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
"originalTotalSales": 9366,
"totalSalesAsBool": true
}
]
Bu tablo, giriş ifadesinin veri türüne göre $toBool işlecinin beklenen davranışını belirtir.
| Değer Türü | Behavior/Output |
|---|---|
| Boolean değeri doğru | Çıkış -> doğru |
| Boole değeri false | Çıkış -> yanlış |
| Herhangi bir Double, Int, Long veya Decimal değeri | Çıkış -> doğru |
| Herhangi bir ISODate değeri | Çıkış -> doğru |
| Null değer | Çıkış -> boş |