Sort Documents by Matched Embedded Documents in Query (possibly with $ positional)

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Done
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • Query
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Currently there does not seem to be a way to sort a collection of Documents based on matched embedded document information. A possible solution would be to reuse the syntax of the $ positional matcher.

      Possible Example:

      doc1 : { name : "Neil", ranks : [ { name : 'fun' , score : 8 }, { name : 'smell' : score : 3 } ]
      doc2 : { name : "Hamburger", ranks : [ { name : 'fun', score : 6 }, { name : 'smell' : score : 5 } ]
      doc2 : { name : "Pete", ranks : [ { name : 'fun', score : 3 } ]
      

      Query

      docs { $query : { ranks : { $elemMatch : { name : 'smell' } } }, $orderby : { ranks.$.score : -1 }}
      

      Should return basically:

      [ doc2, doc1 ]
      

      Could also be combined with other feature requests that would limit the document to the matched embedded document so that the $orderby could sort of off the only (or multiple) results. ( $orderby : ranks.score )

            Assignee:
            Backlog - Query Team (Inactive)
            Reporter:
            Dave Krupinski
            Votes:
            4 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: