Would like the ability to use the index for sorting an array of sub-documents (aggregation pipeline)

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Correct me if I'm wrong, I believe that it is currently impossible to sort an array of sub-documents while using its index.

      Take for example this collection of exactly 1 document (and imagine it has 10,000 sub-documents in the `readers` array.) The collection has an index on `readers.name`.

      {_id: 1, book: "The Great Book", readers: [
      {_id: 1, name: "Joe", sex: "male"},{_id: 2, name: "Jane", sex: "female"}]}

      As soon as `$unwind` is used in an aggregation pipeline, the aggregation pipeline stops using all indexes.

      • My first step in the aggregation pipeline would be to use `$match` to match ` {book: "The Great Book}

        `.

      • My second step in the aggregation pipeline would be to `$unwind` the `readers`. ====> (So, if I understand correctly, I won't be able to make use of indexes after this step!?)
      • My third step in the aggregation pipeline would be to sort by `"readers.name"`. (If I understand correctly, this third step cannot make use of an index?!)</p>

      Is there a different way (that I cannot see) of doing this sort without clogging the memory (by not using the index)?

            Assignee:
            Mark Agarunov (Inactive)
            Reporter:
            Emil Arsa
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: