MongoDB聚合運(yùn)算符$toBool詳解
MongoDB聚合運(yùn)算符:$toBool
$toBool
聚合運(yùn)算符將指定的值轉(zhuǎn)換為布爾類(lèi)型boolean。
語(yǔ)法
{ $toBool: <expression> }
$toBool
接受任何有效的表達(dá)式。
$toBool
是$convert
表達(dá)式的簡(jiǎn)寫(xiě)形式:
{ $convert: { input: <expression>, to: "bool" } }
使用
下表列出了可轉(zhuǎn)換為布爾值的類(lèi)型:
輸入類(lèi)型 | 規(guī)則 |
---|---|
Array | 返回ture |
Binary data | Returns true |
Boolean | 直接返回 |
Code | 返回true |
Date | 返回true |
Decimal | 0返回false,非0返回true |
Double | 0返回false,非0返回true |
Integer | 0返回false,非0返回true |
JavaScript | 返回true |
Long | 0返回false,非0返回true |
MaxKey | 返回true |
MinKey | 返回true |
Null | 返回null |
Object | 返回true |
ObjectId | 返回true |
Regular expression | 返回true |
String | 返回true |
Timestamp | 返回true |
下表列出了一些轉(zhuǎn)換為布爾值的示例:
示例 | 結(jié)果 |
---|---|
{$toBool: false} | false |
{$toBool: 1.99999} | true |
{$toBool: NumberDecimal("5")} | true |
{$toBool: NumberDecimal("0")} | false |
{$toBool: 100} | true |
{$toBool: ISODate("2018-03-26T04:38:28.044Z")} | true |
{$toBool: "false"} | true |
{$toBool: ""} | true |
{$toBool: null} | null |
舉例
使用下面的腳本創(chuàng)建orders
集合:
db.orders.insertMany( [ { _id: 1, item: "apple", qty: 5, shipped: true }, { _id: 2, item: "pie", qty: 10, shipped: 0 }, { _id: 3, item: "ice cream", shipped: 1 }, { _id: 4, item: "almonds", qty: 2, shipped: "true" }, { _id: 5, item: "pecans", shipped: "false" }, //注意:所有的字符串都轉(zhuǎn)換為true { _id: 6, item: "nougat", shipped: "" } //注意:所有的字符串都轉(zhuǎn)換為true ] )
下面是對(duì)訂單集合orders
的聚合操作,先將已發(fā)貨的訂單shipped
轉(zhuǎn)換為布爾值,然后再查找未發(fā)貨的訂單:
//定義shippedConversionStage階段,添加轉(zhuǎn)換后的發(fā)貨標(biāo)志字段`convertedShippedFlag` //因?yàn)樗械淖址紩?huì)被轉(zhuǎn)換為true,所以要對(duì)字符串"false"做個(gè)特殊處理 shippedConversionStage = { $addFields: { convertedShippedFlag: { $switch: { branches: [ { case: { $eq: [ "$shipped", "false" ] }, then: false } , { case: { $eq: [ "$shipped", "" ] }, then: false } ], default: { $toBool: "$shipped" } } } } }; // 定義文檔過(guò)濾階段,過(guò)濾出沒(méi)有發(fā)貨的訂單 unshippedMatchStage = { $match: { "convertedShippedFlag": false } }; db.orders.aggregate( [ shippedConversionStage, unshippedMatchStage ] )
執(zhí)行的結(jié)果為:
{ "_id" : 2, "item" : "pie", "qty" : 10, "shipped" : 0, "convertedShippedFlag" : false }
{ "_id" : 5, "item" : "pecans", "shipped" : "false", "convertedShippedFlag" : false }
{ "_id" : 6, "item" : "nougat", "shipped" : "", "convertedShippedFlag" : false }
到此這篇關(guān)于MongoDB聚合運(yùn)算符:$toBool的文章就介紹到這了,更多相關(guān)MongoDB聚合運(yùn)算符內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mongodb如何開(kāi)啟用戶(hù)訪問(wèn)控制詳解
默認(rèn)啟動(dòng) MongoDB 服務(wù)時(shí)沒(méi)有任何參數(shù),可以對(duì)數(shù)據(jù)庫(kù)任意操 作,而且可以遠(yuǎn)程訪問(wèn)數(shù)據(jù)庫(kù),所以推薦開(kāi)發(fā)階段可以不設(shè)置任何參數(shù),但對(duì)于生產(chǎn)環(huán)境還是要仔細(xì)考慮一下安全方面的因素,下面就介紹了Mongodb開(kāi)啟用戶(hù)訪問(wèn)控制的相關(guān)資料。2017-01-01淺析Mongodb性能優(yōu)化的相關(guān)問(wèn)題
數(shù)據(jù)庫(kù)性能對(duì)軟件整體性能的影響是不言而喻的,那么,當(dāng)我們使用MongoDB時(shí)改如何提高數(shù)據(jù)庫(kù)性能呢?這篇文章通過(guò)范式化與反范式化、填充因子的使用和索引的使用三個(gè)方面來(lái)談了談Mongodb性能優(yōu)化的相關(guān)問(wèn)題,有需要的朋友們下面來(lái)一起看看吧。2016-10-10ubuntu安裝mongodb創(chuàng)建賬號(hào)和庫(kù)及添加坐標(biāo)索引的流程分析
這篇文章主要介紹了ubuntu安裝mongodb創(chuàng)建賬號(hào)和庫(kù)及添加坐標(biāo)索引的流程分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10MongoDb的"not master and slaveok=false"錯(cuò)誤及解決方法
今天小編就為大家分享一篇關(guān)于MongoDb的"not master and slaveok=false"錯(cuò)誤及解決方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10在Linux服務(wù)器中配置mongodb環(huán)境的步驟
這篇文章主要介紹了在Linux服務(wù)器中配置mongodb環(huán)境的步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07