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

Sorting on regex field should not depend on the order of regex options

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Query Optimization

      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 }
      

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            bjori Hannes Magnusson
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: