-3

I wish to find all the items that match some field - but in addition, exclude from the results those items that match multiple regular expressions. what's the syntax here?

in other words: find X and (not regex y) and (not regex z) etc.

a similar question with a single phrase and some additional data can be found here: Mongo regex for "not match" or inverse

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79

2 Answers2

1

One way, using a single regex pattern:

db.getCollection('mycollection').find({fieldName: {$regex: "^(?!Y|Z$)X$"}})

The regex pattern says to match:

^             from the start of the field
    (?!Y|Z$)  do not see patterns Y or Z
    X         match pattern X
$             end of the field
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

it should look like this:

db.getCollection('mycollection').find({$and: [{"fieldname":"some-value-I-want"},{"fieldname":{$not: /some-regex-I-dont-want-1/}} , {"fieldname":{$not: /some-regex-I-dont-want-2/}}]})
FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79