[SERVER-26992] Sorting on regex field should not depend on the order of regex options Created: 11/Nov/16  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Hannes Magnusson Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-26991 Inconsistent handling of RegEx options Closed
Assigned Teams:
Query Optimization
Participants:

 Description   

The BSON Regex type is sorted as so:

  • Compare the match expression
  • If the match expression is equal, compare the options

The options comparisons seems to be the reason why some code, sometimes, alphabetize the valid regex options, so the options "xi" are ordered before "ms", because some tools (such as the shell) rewrite the "xi" options to "ix", and therefore sorting before "ms".
Some code within the server alludes to this to, but does not itself verify, or even guarantee, the order when printing or otherwise exporting such types.

Besides it being a tad weird to rely on alphabetically ordered options for sorting, I think the server itself should be ordering the options is wants the values if it deems their order to be significant, not relying on the clients doing so:

> db.regex.insert({reg: new RegExp("hello", "yimg")})
WriteResult({ "nInserted" : 1 })
> db.regex.insert({reg: /hello/yimg})
WriteResult({ "nInserted" : 1 })
> db.regex.find({reg: {$type: "regex"}}).sort({"reg": 1})
{ "_id" : ObjectId("5825221bad899b26f25b1de7"), "reg" : /hello/gimy }
{ "_id" : ObjectId("5825221fad899b26f25b1de8"), "reg" : /hello/gimy }


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