MongoDB聚合運(yùn)算符$toBool詳解
MongoDB聚合運(yùn)算符:$toBool
$toBool聚合運(yùn)算符將指定的值轉(zhuǎn)換為布爾類型boolean。
語法
{
$toBool: <expression>
}$toBool接受任何有效的表達(dá)式。
$toBool是$convert表達(dá)式的簡(jiǎn)寫形式:
{ $convert: { input: <expression>, to: "bool" } }使用
下表列出了可轉(zhuǎn)換為布爾值的類型:
| 輸入類型 | 規(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" }
}
}
}
};
// 定義文檔過濾階段,過濾出沒有發(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)文章
ubuntu安裝mongodb創(chuàng)建賬號(hào)和庫及添加坐標(biāo)索引的流程分析
這篇文章主要介紹了ubuntu安裝mongodb創(chuàng)建賬號(hào)和庫及添加坐標(biāo)索引的流程分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
MongoDb的"not master and slaveok=false"錯(cuò)誤及解決方法
今天小編就為大家分享一篇關(guān)于MongoDb的"not master and slaveok=false"錯(cuò)誤及解決方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
在Linux服務(wù)器中配置mongodb環(huán)境的步驟
這篇文章主要介紹了在Linux服務(wù)器中配置mongodb環(huán)境的步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

