[SERVER-55583] Difference between RegExp and raw // syntax for $regexFind captures Created: 28/Mar/21  Updated: 27/Oct/23  Resolved: 29/Mar/21

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 4.4.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Daniel Pasette (Inactive) Assignee: Backlog - Query Optimization
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to COMPASS-4737 Editing view causes $regexFind to be ... Open
Assigned Teams:
Query Optimization
Operating System: ALL
Participants:

 Description   

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.



 Comments   
Comment by Daniel Pasette (Inactive) [ 29/Mar/21 ]

Thanks Max. Showed up because compass is rewriting the expression when editing a view definition.

Comment by Max Hirschhorn [ 29/Mar/21 ]

The backslash character must be escaped so it becomes a literal backslash when using RegExp constructor with a string literal.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping

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

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