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

Implement Positional Operator $ for Sorting

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 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

      https://gist.github.com/e9182ceee4dc9de26f15

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            jay@exercise.com Jarom Severson
            Votes:
            10 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated: