mongodb how to query with nand operator?

强颜欢笑 提交于 2020-08-23 07:57:24

问题


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

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

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


回答1:


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

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"}}]})

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"}}]}}])



回答2:


This should fetch you the results

{
    $or: [
        {'a': { $ne: 1} },
        {'b': { $ne: 2} }
    ]
}

However, this expression did not yield the desired result in my case so I tried this-

{
     $nor: [
       {
         $and: [
             {a: 1},
             {b: 2}
         ]
       }
    ]
}



回答3:


there is a very simple trick to create NAND with mongoDB:

$nor : { $and [..., ...] }



回答4:


you can think other way in mongodb with available options.

db.Collection.find({'arrary.a':{$exists:false},'arrary.b':{$exists:false}})

hope this will help you...



来源:https://stackoverflow.com/questions/20810698/mongodb-how-to-query-with-nand-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!