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

Difference between RegExp and raw // syntax for $regexFind captures

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.4.4
    • Component/s: Querying
    • None
    • Query Optimization
    • ALL

      The captures are different when using RegExp vs // syntax.

      Here's the output of a terminal session:

      > db.foo.insert({job:"Engineering [122050] (open)"})
      > db.foo.aggregate({$addFields: { returnObject: { $regexFind: { input: "$job", regex: RegExp('\[(.*)\]') }}}})
      [ { _id: ObjectId("6060b22151a815916d99ba2f"),
          job: 'Engineering [122050] (open)',
          returnObject: { match: '(', idx: 21, captures: [] } } ]>
      > db.foo.aggregate({$addFields: { returnObject: { $regexFind: { input: "$job", regex: /\[(.*)\]/} }}})
      [ { _id: ObjectId("6060b22151a815916d99ba2f"),
          job: 'Engineering [122050] (open)',
          returnObject: { match: '[122050]', idx: 12, captures: [ '122050' ] } } ]
      

      I am pretty sure that the syntax should be equivalent, but note the difference in the captures between the two queries. This is on an Atlas free tier (v4.4.4) cluster.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            dan@mongodb.com Daniel Pasette (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: