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)文章
MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì)功能
MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由C++語言編寫。旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。。本文給大家分享MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì),需要的朋友可以參考下2019-11-11
MongoDB用Mongoose得到的對(duì)象不能增加屬性完美解決方法(兩種)
本文給大家分享兩種解決方案解決MongoDB用Mongoose得到的對(duì)象不能增加屬性問題,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-11-11
MongoDB使用小結(jié):一些不常見的經(jīng)驗(yàn)分享
最近一年忙碌于數(shù)據(jù)處理相關(guān)的工作,跟MongoDB打交道極多,以下為實(shí)踐過程中的Q&A,后續(xù)會(huì)不定期更新補(bǔ)充2017-03-03
使用Locust對(duì)MongoDB進(jìn)行負(fù)載測(cè)試的操作步驟
Locust是一款使用Python開發(fā)的開源性能測(cè)試工具,支持分布式,可在多臺(tái)主機(jī)上對(duì)系統(tǒng)持續(xù)發(fā)送請(qǐng)求,本文給大家介紹了使用Locust對(duì)MongoDB進(jìn)行負(fù)載測(cè)試的操作步驟,文中通過圖文結(jié)合的方式介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
Mongodb 如何將時(shí)間戳轉(zhuǎn)換為年月日日期
這篇文章主要介紹了Mongodb 如何將時(shí)間戳轉(zhuǎn)換為年月日日期,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

