MongoDB聚合运算符:$not

文章目录

$not聚合运算符用于将指定布尔表达式的值取反,比如,表达式的值为 true$not返回 false;表达式的值为 false$not则返回 true

语法

js 复制代码
{ $not: [ <expression> ] }

使用

false外,null0undefined都被认为是false,其他值包括非零值和数组都被认为是true,如:

例子 结果
{ $not: [ true ] } false
{ $not: [ [ false ] ] } false
{ $not: [ false ] } true
{ $not: [ null ] } true
{ $not: [ 0 ] } true

举例

inventory集合有下列文档:

json 复制代码
{ "_id" : 1, "item" : "abc1", "description": "product 1", "qty": 300 }
{ "_id" : 2, "item" : "abc2", "description": "product 2", "qty": 200 }
{ "_id" : 3, "item" : "xyz1", "description": "product 3", "qty": 250 }
{ "_id" : 4, "item" : "VWZ1", "description": "product 4", "qty": 300 }
{ "_id" : 5, "item" : "VWZ2", "description": "product 5", "qty": 180 }

下面的聚合操作使用$not运算符来判断qty是否不大于(小于等于)250

js 复制代码
db.inventory.aggregate(
   [
     {
       $project:
          {
            item: 1,
            result: { $not: [ { $gt: [ "$qty", 250 ] } ] }
          }
     }
   ]
)

操作返回下面的结果:

json 复制代码
{ "_id" : 1, "item" : "abc1", "result" : false }
{ "_id" : 2, "item" : "abc2", "result" : true }
{ "_id" : 3, "item" : "xyz1", "result" : true }
{ "_id" : 4, "item" : "VWZ1", "result" : false }
{ "_id" : 5, "item" : "VWZ2", "result" : true }
相关推荐
龙亘川15 分钟前
【课程5.1】城管住建核心功能需求分析:市政设施、市容秩序等场景痛点拆解
数据库·oracle·智慧城市·城管住建
飞鸟真人25 分钟前
Redis面试常见问题详解
数据库·redis·面试
fanruitian1 小时前
Springboot项目父子工程
java·数据库·spring boot
super_lzb1 小时前
mybatis拦截器ParameterHandler详解
java·数据库·spring boot·spring·mybatis
CV工程师的自我修养1 小时前
数据库出现死锁了。还不知道什么原因引起的?快来看看吧!
数据库
码界奇点2 小时前
灵活性与高性能兼得KingbaseES 对 JSON 数据的全面支持深度解析
数据库·json·es
2501_941871452 小时前
面向微服务链路追踪与全局上下文管理的互联网系统可观测性设计与多语言工程实践分享
大数据·数据库·python
·云扬·2 小时前
MySQL单机多实例部署两种实用方法详解
数据库·mysql·adb
odoo中国2 小时前
Pgpool-II 在 PostgreSQL 中的用例场景与优势
数据库·postgresql·中间件·pgpool
男孩李2 小时前
postgres数据库常用命令介绍
数据库·postgresql