[DOCS-12420] Docs for SERVER-13779: Allow $not to be applied to $regex (currently only /regex/ syntax is allowed) Created: 01/Feb/19  Updated: 13/Nov/23  Resolved: 19/Feb/19

Status: Closed
Project: Documentation
Component/s: manual, Server
Affects Version/s: None
Fix Version/s: 4.1.8, 4.0.7, Server_Docs_20231030, Server_Docs_20231106, Server_Docs_20231105, Server_Docs_20231113

Type: Task Priority: Major - P3
Reporter: Kay Kim (Inactive) Assignee: Kay Kim (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
documents SERVER-13779 Allow $not to be applied to $regex (c... Closed
Participants:
Days since reply: 4 years, 46 weeks, 2 days ago
Epic Link: DOCS: 4.2 Server/Tools

 Description   

Description

Description:

Remove section about restrictions on $not and $regex on this page: https://docs.mongodb.com/manual/reference/operator/query/not/index.html

Engineering Ticket Description:

Executing the following queries I was expecting the same result but apparently they are dealt differently:

2 scenarios

  • matching

    nair(mongod-2.7.0-pre-) test> db.system.namespaces.find({ name:/\$/} )
    {
      "name": "test.data.$_id_"
    }
    {
      "name": "test.difference.$_id_"
    }
    {
      "name": "test.posts.$_id_"
    }
    {
      "name": "test.kunal.$_id_"
    }
    {
      "name": "test.maha_example.$_id_"
    }
    

    has the same result has using $regex operator

    nair(mongod-2.7.0-pre-) test> db.system.namespaces.find({ name:{ $regex: "\\$"}} )
    {
      "name": "test.data.$_id_"
    }
    {
      "name": "test.difference.$_id_"
    }
    {
      "name": "test.posts.$_id_"
    }
    {
      "name": "test.kunal.$_id_"
    }
    {
      "name": "test.maha_example.$_id_"
    }
    

  • not matching query

    nair(mongod-2.7.0-pre-) test> db.system.namespaces.find({ name: {$not: /\$/}} )
    {
      "name": "test.system.indexes"
    }
    {
      "name": "test.data"
    }
    {
      "name": "test.difference"
    }
    {
      "name": "test.posts"
    }
    {
      "name": "test.kunal"
    }
    {
      "name": "test.maha_example"
    }
    {
      "name": "test.system.profile",
      "options": {
        "capped": true,
        "size": 1048576
      }
    }
    

    But using $regex operator fails

    nair(mongod-2.7.0-pre-) test> db.system.namespaces.find({ name: {$not:  { $regex: "\\$"}}} )
    error: {
      "$err": "Can't canonicalize query: BadValue $not cannot have a regex",
      "code": 17287
    }
    

Is this the expected behaviour ? and if so why ?

Looking deeper in the code I can also see the following comment:
mongo/db/matcher/expression_parser_tree.cpp:103

...
         // TODO: this seems arbitrary?
         // tested in jstests/not2.js
         for ( unsigned i = 0; i < theAnd->numChildren(); i++ )
             if ( theAnd->getChild(i)->matchType() == MatchExpression::REGEX )
                 return StatusWithMatchExpression( ErrorCodes::BadValue, "$not cannot have a regex" );
...

and looking int the jstests it makes even less sense:

jstests/core/not2.js

  49 check( {i:{$not:/a/}}, "b" );
  50 check( {i:{$not:/(a|b)/}}, "", 0 );
  51 check( {i:{$not:/a/,$regex:"a"}}, "", 0 );
  52 check( {i:{$not:/aa/}}, "a", 2 );
  53 fail( {i:{$not:{$regex:"a"}}} );
  54 fail( {i:{$not:{$options:"a"}}} );

Scope of changes

Impact to Other Docs

MVP (Work and Date)

Resources (Scope or Design Docs, Invision, etc.)



 Comments   
Comment by Githook User [ 25/Mar/19 ]

Author:

{'name': 'Kay Kim', 'username': 'kay-kim', 'email': 'kay.kim@10gen.com'}

Message: DOCS-12420: 4.0.7 and operator expression (part2)
Branch: v4.0
https://github.com/mongodb/docs/commit/9251daaa009fc888ad84a7ae3f4939abcec22386

Comment by Githook User [ 25/Mar/19 ]

Author:

{'name': 'Kay Kim', 'username': 'kay-kim', 'email': 'kay.kim@10gen.com'}

Message: DOCS-12420: 4.0.7 $not and $regex operator expression
Branch: v4.0
https://github.com/mongodb/docs/commit/b3bd8042677842f494b43eb7d62f891f7fa78821

Comment by Githook User [ 25/Mar/19 ]

Author:

{'email': 'kay.kim@10gen.com', 'name': 'Kay Kim', 'username': 'kay-kim'}

Message: DOCS-12420: 4.0.7 and operator expression (part2)
Branch: v4.0.7
https://github.com/mongodb/docs/commit/90f64156c181cafa8ec38617dada43e88a8d8c5f

Comment by Githook User [ 25/Mar/19 ]

Author:

{'email': 'kay.kim@10gen.com', 'name': 'Kay Kim', 'username': 'kay-kim'}

Message: DOCS-12420: 4.0.7 $not and $regex operator expression
Branch: v4.0.7
https://github.com/mongodb/docs/commit/e04cf4f42c3eb6564db52cd79552fb95b809f33a

Comment by Githook User [ 19/Feb/19 ]

Author:

{'name': 'Kay Kim', 'email': 'kay.kim@10gen.com', 'username': 'kay-kim'}

Message: DOCS-12420: 4.2 and operator expression (part2)
Branch: master
https://github.com/mongodb/docs/commit/472e801a35efab1af5d27c047ba596bff02adef2

Comment by Githook User [ 19/Feb/19 ]

Author:

{'name': 'Kay Kim', 'username': 'kay-kim', 'email': 'kay.kim@10gen.com'}

Message: DOCS-12420: 4.0.7 and operator expression (part2)
Branch: v4.0.7
https://github.com/mongodb/docs/commit/44559187f8e0a328c89b742e87303993c9883a17

Comment by Githook User [ 19/Feb/19 ]

Author:

{'name': 'Kay Kim', 'email': 'kay.kim@10gen.com', 'username': 'kay-kim'}

Message: DOCS-12420: 4.0.7 $not and $regex operator expression
Branch: v4.0.7
https://github.com/mongodb/docs/commit/c0accd744d2eaf046f5af3130e5becf0bc47e82b

Comment by Githook User [ 19/Feb/19 ]

Author:

{'name': 'Kay Kim', 'username': 'kay-kim', 'email': 'kay.kim@10gen.com'}

Message: DOCS-12420: 4.2 $not and $regex operator expression
Branch: master
https://github.com/mongodb/docs/commit/e79a02a6f8d85f8dbf140e7ab18b0acbe30972dc

Comment by James Wahlin [ 19/Feb/19 ]

We have decided to backport this change to MongoDB 4.0 (4.0.7) as well as it is required by a backport of SERVER-39019, which addresses a correctness issue.

Generated at Thu Feb 08 08:05:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.