query planner doesn't choose index with multi-key index with partialFilterExpression

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • ALL
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None

      This might be a shaded line between a bug and a feature request. Or maybe a documentation request because it's not clear why it doesn't work.

       

      We have a document as follows:

      ```

      {   _id: ObjectId(...),   account: ObjectId(...),   names: ["name1", "name2"],   ... }

      ```

       

      We make a compound + multikey + partial filter expression index as follows:

      ```

      { account: 1, names: 1}

      , {partialFilterExpression: {"names":

      {"$exists": 1}

      }}

      ```

      This works and searches on account + names use the index.

      But names is in a minority of documents, so if we have a document with an empty array, it indexes the document, bloating the index size. (we should probably make them `null` indeed).

      So we try this clever index:

      ```

      { account: 1, names: 1}

      , {partialFilterExpression: {"names.0":

      {"$exists": 1}

      }}

      ```

      Now the index is created only on arrays where `names.0` exists. However when we query on `account` and `names` it doesn't use the index. Either it doesn't use this because that's just not how multikey indexes work - or it doesn't work because the query planner doesn't see `names.0` as a match for the query so it rejects the idea of using the index.

            Assignee:
            Unassigned
            Reporter:
            Ewan Higgs
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: