Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-3486

Regex not matching documents with pattern

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Query Operations
    • Labels:
      None

      We have tested our regex pattern with mongodb sql clients to verify that the pattern works. However, in pymongo the pattern does not work for all documents. Im creating this ticket to find out if this is a bug or find if i am using the client wrong.

      I have played around with creating an index to create functionality to match a document based off the value of the key `position`. This is in the top level of a json document.

      ```
      "position": "Knee Flexion Supine",
      ```

      After loading documents, I created a test case to match all documents based upon a string.

      *Example*
      Input:  "Knee Flexion Supine Right Max Force"
      Transforms to pattern:  .(Knee|Flexion|Supine)

      Ive tried many ways, with find, find_one, aggregate, they all seem to work in their own way but does not match all documents. Although the sql client does.

      CODE BELOW

      def test_get_by_keywords(collection, tests, document_data):             
      collection.create_index([('position', pymongo.TEXT)],                                  unique=False)
          collection.insert_many(vald_athlete_tests)    
          for cl_test in performalyze_athlete_tests:        
              test = TestRequest(**cl_test)
              doc = collection.aggregate([
                  {"$match": {"position": {"$regex":               convert_to_keyword_search(cl_test.testName)}}}
      ])

      Pattern builder function
      ```
      def convert_to_keyword_search(keywords: List):

      1. 'Knee|Flexion|Seated|Right|Force|Metrics'\
          if'Right'inkeywords:
            pattern = keywords.partition(' Right')[0]
          if'Left'inkeywords:
            pattern = keywords.partition(' Left')[0]
          returnre.compile(f".({'|'.join(pattern.split(' '))})")
        ```

            Assignee:
            steve.silvester@mongodb.com Steve Silvester
            Reporter:
            dillan.teagle.va@gmail.com dillan teagle
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: