What query are you using that does not produce the correct results? What version of MongoDB are you using? Your $not request is not a valid request in MongoDB 2.6:
> db.amon.find({ "$not" : { "code" : /^AAA/, "name" : { "$in" : ["BB", "CC"] } } }) error: { "$err" : "Can't canonicalize query: BadValue unknown top level operator: $not", "code" : 17287 }
Here is an example that does what you want:
> db.amon.find().pretty() { "_id" : ObjectId("53ea66bdf9b63e0dd3ca1a18"), "code" : "AAA", "name" : "AA" } { "_id" : ObjectId("53ea66c1f9b63e0dd3ca1a19"), "code" : "AAA", "name" : "BB" } { "_id" : ObjectId("53ea66c3f9b63e0dd3ca1a1a"), "code" : "AAA", "name" : "CC" } { "_id" : ObjectId("53ea66d3f9b63e0dd3ca1a1b"), "code" : "BBB", "name" : "AA" } { "_id" : ObjectId("53ea66d6f9b63e0dd3ca1a1c"), "code" : "BBB", "name" : "BB" } { "_id" : ObjectId("53ea66daf9b63e0dd3ca1a1d"), "code" : "BBB", "name" : "CC" } > db.amon.find({ "$or" : [ { "code" : { "$not" : /^AAA/ } }, { "name": { "$not" : { "$in" : ["BB", "CC"] } } } ] }) { "_id" : ObjectId("53ea66bdf9b63e0dd3ca1a18"), "code" : "AAA", "name" : "AA" } { "_id" : ObjectId("53ea66d3f9b63e0dd3ca1a1b"), "code" : "BBB", "name" : "AA" } { "_id" : ObjectId("53ea66d6f9b63e0dd3ca1a1c"), "code" : "BBB", "name" : "BB" } { "_id" : ObjectId("53ea66daf9b63e0dd3ca1a1d"), "code" : "BBB", "name" : "CC" }
An easy way to record this query is to use DeMorgan Laws : the complement to the intersection (s) is the union of the additions, Since you are looking for documents that do not satisfy (AAA code) and (the name is one of BB or CC), the condition that they satisfy is not is ((code is AAA) and (name is one of BB or CC)) = (code is not AAA) or (name is not BB or CC).
source share