MongoDB聚合运算符:$toBool

devtools/2024/9/24 7:28:56/

MongoDB聚合运算符:$toBool

文章目录

  • MongoDB聚合运算符:$toBool
    • 语法
    • 使用
    • 举例

$toBool聚合运算符将指定的值转换为布尔类型boolean。

语法

{$toBool: <expression>
}

$toBool接受任何有效的表达式。

$toBool$convert表达式的简写形式:

{ $convert: { input: <expression>, to: "bool" } }

使用

下表列出了可转换为布尔值的类型:

输入类型规则
Array返回ture
Binary dataReturns true
Boolean直接返回
Code返回true
Date返回true
Decimal0返回false,非0返回true
Double0返回false,非0返回true
Integer0返回false,非0返回true
JavaScript返回true
Long0返回false,非0返回true
MaxKey返回true
MinKey返回true
Null返回null
Object返回true
ObjectId返回true
Regular expression返回true
String返回true
Timestamp返回true

下表列出了一些转换为布尔值的示例:

示例结果
{$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

举例

使用下面的脚本创建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" },  //注意:所有的字符串都转换为true{ _id: 6, item: "nougat", shipped: ""  }       //注意:所有的字符串都转换为true
] )

下面是对订单集合orders的聚合操作,先将已发货的订单shipped转换为布尔值,然后再查找未发货的订单:

//定义shippedConversionStage阶段,添加转换后的发货标志字段`convertedShippedFlag`
//因为所有的字符串都会被转换为true,所以要对字符串"false"做个特殊处理
shippedConversionStage = {$addFields: {convertedShippedFlag: {$switch: {branches: [{ case: { $eq: [ "$shipped", "false" ] }, then: false } ,{ case: { $eq: [ "$shipped", "" ] }, then: false }],default: { $toBool: "$shipped" }}}}
};// 定义文档过滤阶段,过滤出没有发货的订单unshippedMatchStage = {$match: { "convertedShippedFlag": false }
};db.orders.aggregate( [shippedConversionStage,unshippedMatchStage
] )

执行的结果为:

{ "_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 }

http://www.ppmy.cn/devtools/32950.html

相关文章

2000-2020年县域创业活跃度数据

2000-2020年县域创业活跃度数据 1、时间&#xff1a;2000-2020年 2、指标&#xff1a;地区名称、年份、行政区划代码、经度、纬度、所属城市、所属省份、年末总人口万人、户籍人口数万人、当年企业注册数目、县域创业活跃度1、县域创业活跃度2、县域创业活跃3 3、来源&#…

ZooKeeper以及DolphinScheduler的用法

目录 一、ZooKeeper的介绍 数据模型 ​编辑 操作使用 ①登录客户端 ​编辑 ②可以查看下面节点有哪些 ③创建新的节点&#xff0c;并指定数据 ④查看节点内的数据 ⑤、删除节点及数据 特殊点&#xff1a; 运行机制&#xff1a; 二、DolphinScheduler的介绍 架构&#…

修改 Windows 服务器远程端口,以阿里云服务器 ECS 为例

一、WinR - mstsc.exe 登录远程服务器 二、WinR - regedit.exe 打开注册表 三、打开注册表 tcp 路径&#xff1a; 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TerminalServer\Wds\rdpwd\Tds\tcp 双击右侧 PortNumber 子健&#xff0c;勾选十进制&a…

同创优配正规股票交易高盛午评:是的,中国交易回来了

查查配中国经济强劲复苏的预期下,港股上演了一场史诗级大反攻! 同创优配是AAA 级诚信经营示范单位,中国人民银行备案认证,天眼查可查询。是一家专注于股票投资、金融服务、及资产管理的专业机构 自1月低点至今,恒生指数累计上涨近21%,恒生科技指数累计上涨逾28%。最近几周,港…

ffmpeg 推流

参考 【windows&&linux环境下实现ffmpeg&&vlc rtsp本地视频、摄像头推流&#xff0c;VLC推拉流】 本地视频 推流 localhost 下载 mediamtx 服务器 https://github.com/bluenviron/mediamtx/releases 下载 ffmpeg https://github.com/BtbN/FFmpeg-Builds/rel…

Java并发编程面试问题与答案

1. 什么是线程安全&#xff1f; 答&#xff1a; 线程安全意味着多个线程可以同时访问一个类的实例而不引起任何问题或不一致的结果。线程安全的代码会通过同步机制来确保所有线程都能正确地访问共享资源。 2. 解释Java中的synchronized关键字。 答&#xff1a; synchronized…

2024-04-30 问AI: 介绍一下VGG16深层网络模型

文心一言 VGG16 是一种深度卷积神经网络&#xff08;CNN&#xff09;模型&#xff0c;由牛津大学的 Visual Geometry Group (VGG) 开发。这个模型在 2014 年的 ImageNet Large Scale Visual Recognition Challenge (ILSVRC) 竞赛中取得了显著的成绩&#xff0c;证明了深度网络…

有关CSS中排版常见问题(清除默认样式问题 + 元素居中问题 + 元素之间的空白问题 + 行内块的幽灵空白问题)

前言&#xff1a;在练习CSS排版的时候&#xff0c;我们经常会遇到一些排版上的问题&#xff0c;那么我们如何去解决这些问题呢&#xff1f;本篇文章给出了一些新手在练习排版时候可能会遇到的问题的解决方案。 ✨✨✨这里是秋刀鱼不做梦的BLOG ✨✨✨想要了解更多内容可以访问我…