This page documents that $not with $regex is disallowed, but this behavior seems counterintuitive from an end-user perspective and perhaps could be reconsidered.
Setup a simple repro:
Using $not with a raw BSON regex works as expected:
But using $not with $regex returns an error:
I tried to do a "Not like" query with following codes :
test = BSON(std::string("$regex")<<myStr<<std::string("$options")<<"i");
test = BSON(std::string("$not")<<test);
test = BSON("x"<<test);
It doesn't work, saying that $not operator and $regex operator do not work together.
However, I tried to build with object builder :
x.appendRegex( "$not" , myStr);
BSONObj test = BSON( "x" << x.obj() );
Meanwhile, the '.jsonString()' of these two objects are the same.
Thus I think it's a bug of the C++ driver.