Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb how to query with nand operator?

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) ?

like image 595
yountae.kang Avatar asked Mar 22 '23 04:03

yountae.kang


2 Answers

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}
         ]
       }
    ]
}
like image 131
mohakjuneja Avatar answered Mar 23 '23 20:03

mohakjuneja


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"}}]}}])
like image 20
Daniel Coupal Avatar answered Mar 23 '23 20:03

Daniel Coupal