[SERVER-2275] Sort Documents by Matched Embedded Documents in Query (possibly with $ positional) Created: 22/Dec/10  Updated: 07/Apr/23  Resolved: 19/May/17

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

Type: Improvement Priority: Minor - P4
Reporter: Dave Krupinski Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 4
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query
Participants:

 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 )



 Comments   
Comment by David Storch [ 19/May/17 ]

Closing as Gone Away per asya's comment above.

Comment by Asya Kamsky [ 17/May/17 ]

This can be done in aggregation framework pipeline.

Generated at Thu Feb 08 02:59:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.