-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Querying
-
Query Execution
Given a collection with the following documents:
{ arr: [ {a: 1, b: 1}, {a: 2, b: 3} ] } { arr: [ {a: 1, b: 8}, {a: 2, b: 7} ] } { arr: [ {a: 1, b: 4}, {a: 2, b: 5} ] }
This new feature would allow the positional operator ($) to be used to sort by a field of the matched documents inside the array. For example, the following query would sort by the "b" field of the inner documents with "a" equal to 2:
db.test.find({"arr.a": 2}).sort({"arr.$.b": -1});
The documents matching documents would then be returned by the cursor in the following order:
{ arr: [ {a: 1, b: 8}, {a: 2, b: 7} ] } { arr: [ {a: 1, b: 4}, {a: 2, b: 5} ] } { arr: [ {a: 1, b: 1}, {a: 2, b: 3} ] }
------
Related posts:
https://groups.google.com/forum/#!topic/mongodb-user/w5ZVI4yraik