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

$elemMatch doesn't fully use compound index

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • ALL

      I might miss something but when I create compound index for nested array of documents, when I filter by fields from those index, only one of them is filtered inside INDEX_SCAN stage, and another field is filtered in FETCH stage

      Sample script:

      db.test.drop()
      for(var i = 0; i < 100; i++){    
         db.test.insert({units: [{jobs: [{jobType: i % 3, status: i % 10 }]}]})
      }
      db.test.createIndex({"units.jobs.status": 1, "units.jobs.jobType": 1})
      db.test
         .find({ "units" : { "$elemMatch" : { "jobs" : { "$elemMatch" : { "jobType" : 0, "status" : 3 } } } }})
         .explain('executionStats')
      

      Last query end up with following INDEX_SCAN:

      "indexBounds" : { "indexBounds" :

      { "units.jobs.status" : [ "[3.0, 3.0]" ], "units.jobs.jobType" : [ "[MinKey, MaxKey]" ] }

      ,

      and result set is filtered with FETCH phase.

       

      Is this expected behavious and if so, what is possible workaround?

       

      Thanks in advance

       

            Assignee:
            edwin.zhou@mongodb.com Edwin Zhou
            Reporter:
            denys.isaiev@synergysports.com Denys Isaiev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: