The Java driver can retrieve Regex-type BSON values from MongoDB under several circumstances:
- When a regex has been stored in a document, then queried
- When a regex query is in progress on another connection and the driver queries $cmd.sys.inprog
- When a regex query is stored in system.profile and the driver queries the profile
These regexes might be intended by their authors to be PCRE because they're intended to run on server. However, they need not always be. We can't make any predictions about the content of a BSON regex or what its author intended it to match.
But the Java driver compiled BSON regexes into java.util.Pattern using Pattern.compile. If a regex can't be compiled, then the whole document is unparsable and there's no workaround. Even if the regex is parsable, we don't know if it will behave as intended, since we don't know if java.util.Pattern matches the flavor the regex's author intended it to run on.
Finally, it's not likely that most regexes retrieved from the server are ever executed client-side, so greedily compiling all regexes is wasteful.
We should add a way to optionally disable compilation. If compilation is disabled, represent the BSON regexes some other way, e.g. a MongoRegex class that contains the uncompiled regex pattern as a string plus its options.
Additionally, add a means of sending a PCRE to the server even if its pattern can't be compiled by Pattern, and/or its flags aren't supported by the Pattern.
With the new API, disable auto-compilation by default, with the option to turn it back on.
- is depended on by
-
DRIVERS-82 Don't compile BSON regexes to native regexes
- Closed