mongodb 如何使用 nand 运算符进行查询? [英] mongodb how to query with nand operator?

查看:47
本文介绍了mongodb 如何使用 nand 运算符进行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 $match 聚合.我试过了.

I'm using $match of aggregation. and I tried this.

    $match : { 
      $not: {
      'A': false,
      'B': 'condition'
      } 
    } 

但不像 nand 那样工作.它的工作原理就像不是 A 也不是 B.如何使用 Not(A 和 B) 进行查询?

but not work like nand. it works like not A and not B. How can I query with Not(A and B) ?

推荐答案

$not 运算符不会反转复杂的表达式.对于复杂的表达式,您需要使用 $and 或 $or.

The $not operator does not invert a complex expression. You need to use $and or $or for complex expressions.

使用逻辑规则,我们知道以下是相同的:

Using logic rules, we know that the following are identical:

    not ( A and B ) = not A or not B

使用 MongoDB 查询语言,您将:

Using the MongoDB query language, you would have:

db.collection.find({$or:[{"a":{"$ne":false}},{"b":{"$ne":"condition"}}]})

OR simplifying the double boolean:

db.collection.find({$or:[{"a":true},{"b":{"$ne":"condition"}}]})

使用 MongoDB 聚合框架,您将:

Using the MongoDB aggregation framework, you would have:

db.collection.aggregate([{"$match":{$or:[{"a":{"$ne":false}},{"b":{"$ne":"condition"}}]}}])

OR again, simplified to:

db.collection.aggregate([{"$match":{$or:[{"a":true},{"b":{"$ne":"condition"}}]}}])

这篇关于mongodb 如何使用 nand 运算符进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆