Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-4805

query { $not : { $regex : /x/ } } doesn't work

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.0.2, 2.5.3
    • Component/s: Querying
    • Environment:
      Windows server 2008 r2 sp1
      Developped with mongo C++ server and Visual Studio 2010
    • Query
    • Windows

      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:

      > t = db.t
      test.t
      > t.drop()
      false
      > t.save({y: "x"})
      { "ok" : 1, "n" : 1 }
      > t.save({y: "z"})
      { "ok" : 1, "n" : 1 }
      

      Using $not with a raw BSON regex works as expected:

      > c.find({y: {$not: /x/}})
      { "_id" : ObjectId("52828c701b91a96125c119ce"), "y" : "z" }
      

      But using $not with $regex returns an error:

      > c.find({y: {$not: {$regex: "x"}}})
      error: { "$err" : "bad query: BadValue $not cannot have a regex", "code" : 16810 }
      

      Original description:

      I tried to do a "Not like" query with following codes :
      BSONObj test;
      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 :
      BSONObjBuilder x;
      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.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            aochenyang aochen
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: