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

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

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None

    Description

      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)?

      Attachments

        Activity

          People

            mark.agarunov Mark Agarunov
            emilio911 Emil Arsa
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: