To create a negating regular expression predicate I essentially need to create the following JSON query:
{ $not : /myregex/ }
Unfortunately this is not possible with BasicDBObject as its toString() method (which essentially calls JSON.serialize(...)) wraps the value into quotation marks in any case which results in the following output:
{ "$not" : "/myregex/" }
This is rejected by the driver with the following message:
error: { "$err" : "invalid use of $not", "code" : 13041 }
The crucial part is the value being wrapped into quotation marks. So I think the driver should not escape the value for a $not key if it is a String as it usually only works with another operator wrapped into yet another DBObject. Would be also cool if a new BasicDbObject("$not", Pattern.compile("myRegex")) would create the correct representation as well. Currently this creates:
{ $not : { $regex : "myRegex", $options : ""}}
which is rejected by the driver as well.