I'm having problem to achieve this.
I have this piece of code which I had tested in Rock Mongo and it works fine.
array(
'TEST' =>
array( '$in' =>
array( new MongoRegex('/^(?!TESTVALUE)/$ig'))
)
)
Above piece of code return me all documents which haven't value "TESTVALUE" for the key "TEST".
Now what I want to achieve?
First I don't know how to write piece of code to fetch all documents which haven't values "TESTVALUE" & "SECONDVALUE" for the key "TEST".
That will be something like this:
array(
'TEST' =>
array( '$in' =>
array( new MongoRegex('/^(?!TESTVALUE)/$ig'),new MongoRegex('/^(?!SECONDVALUE)/$ig') )
)
)
And also I will need above piece of code written in PHP.
Any kind of help or suggestions is welcome and will be appreciated.
Thanks in advance
I don't think you can use $in like that, but this should work:
'TEST' => array('$and' => array(
array('$regex' => new MongoRegex('/^(?!TESTVALUE)/$ig'),
array('$regex' => new MongoRegex('/^(?!TESTVALUE)/$ig')))
something to that effect (untested)
Your queries and your description seem to contradict each other. If you want a query for all documents which don't have some string as the field value, why are you using regexes? Do you not want the value to be a substring of the field value? If you just want to do a query like "find documents where the value of the TEST
field is not "X" or "Y", use $nin
("not in"):
db.collection.find({ "TEST" : { "$nin" : ["X", "Y"] } })
If the field should match multiple regexes, then either combine them into a single regex or combine the conditions with $and
.