问题
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