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

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

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Minor - P4 Minor - P4
    • None
    • None
    • Querying
    • None
    • Query

    Description

      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 )

      Attachments

        Activity

          People

            backlog-server-query Backlog - Query Team (Inactive)
            dave@davekrupinski.com Dave Krupinski
            Votes:
            4 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: