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

Plan for query with negation predicates below an $elemMatch value not considered

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.7.5
    • Affects Version/s: 2.6.0, 2.6.3
    • Component/s: Querying
    • Fully Compatible
    • ALL
    • Hide
      
      db.elem.insert({arr: [ 1, 2, 3], a: {arr: [9,8,7]}, b:3 })
      db.elem.insert({arr: [ 4,5,6], a: {arr: [19,18,17]}, b:5 })
      db.elem.ensureIndex({arr:1})
      db.elem.ensureIndex({"a.arr":1})
      db.elem.ensureIndex({b:1})
      db.elem.ensureIndex({arr:1, b: 1})
      db.elem.find({b: {$gt: 3}, arr: {$elemMatch:{$gt: 5}}}).explain(true)
      db.elem.find({b: {$gt: 3}, arr: {$elemMatch:{$ne: 5}}}).explain(true)
      
      Show
      db.elem.insert({arr: [ 1, 2, 3], a: {arr: [9,8,7]}, b:3 }) db.elem.insert({arr: [ 4,5,6], a: {arr: [19,18,17]}, b:5 }) db.elem.ensureIndex({arr:1}) db.elem.ensureIndex({"a.arr":1}) db.elem.ensureIndex({b:1}) db.elem.ensureIndex({arr:1, b: 1}) db.elem.find({b: {$gt: 3}, arr: {$elemMatch:{$gt: 5}}}).explain(true) db.elem.find({b: {$gt: 3}, arr: {$elemMatch:{$ne: 5}}}).explain(true)

      Part of the query planner's index selection phase is to collect all paths over which the query has predicates (see the mongo::QueryPlannerIXSelect::getFields function). The implementation ignores negation predicates inside an $elemMatch value expression. The consequence is that the planner may fail to generate plans which index negation predicates inside $elemMatch value.

      Original Description

      A query containing an $elemMatch with negation predicates is not considereding all plans.

      The Plan with an index which has an array, which the query includes a $elemMatch with a negation predicate is not considered. A similar query without the negation predicate does include the Plans.

      Note that only the plan for b index is considered (see second query result from the steps)

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            jonathan.abrahams Jonathan Abrahams
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: