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 }
相关推荐
tzhou644525 小时前
MySQL备份与恢复
数据库·mysql·adb
一过菜只因5 小时前
MySql Jdbc
android·数据库·mysql
思成不止于此5 小时前
MySQL 查询实战(三):排序与综合练习
数据库·笔记·学习·mysql
茅坑的小石头5 小时前
数据库表设计,概念模型、逻辑模型、物理模型的区别,目标、主要内容、所处阶段、面向人群,数据库无关性
数据库
tebukaopu1485 小时前
mysql数据备份还原
数据库·mysql
zyxqyy&∞5 小时前
mysql代码小练-2
数据库·mysql
JIngJaneIL6 小时前
基于Java非遗传承文化管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
+VX:Fegn08956 小时前
计算机毕业设计|基于springboot + vue心理健康管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
only-qi7 小时前
Redis如何应对 Redis 大 Key 问题
数据库·redis·缓存
muxin-始终如一8 小时前
消息丢失场景和解决方案
数据库·中间件·消息丢失