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

Query with $elemMatch using a compound multikey index can generate incorrect results

    • ALL
    • Hide
      db.test.drop();
      db.test.insert({ "__e" : [ { "t" : 1, "n" : 25 }, { "t" : 3, "n" : 59 } ] });
      db.test.find({'__e' : {$elemMatch : {'t' : {$gte : 2, $lt : 4}, 'n' : 25}}});
      #no record returned as expected
      
      db.test.ensureIndex({'__e.t' : 1, '__e.n' : 1});
      db.test.find({'__e' : {$elemMatch : {'t' : {$gte : 2, $lt : 4}, 'n' : 25}}});
      #record returned (BUG)
      
      Show
      db.test.drop(); db.test.insert({ "__e" : [ { "t" : 1, "n" : 25 }, { "t" : 3, "n" : 59 } ] }); db.test.find({ '__e' : {$elemMatch : { 't' : {$gte : 2, $lt : 4}, 'n' : 25}}}); #no record returned as expected db.test.ensureIndex({ '__e.t' : 1, '__e.n' : 1}); db.test.find({ '__e' : {$elemMatch : { 't' : {$gte : 2, $lt : 4}, 'n' : 25}}}); #record returned (BUG)

      Issue Status as of April 21, 2014

      ISSUE SUMMARY
      When using $elemMatch on a compound multikey index, the result set is not always filtered correctly and may in certain edge cases return additional results that do not match the query. See the comments in this ticket for more details and an example.

      USER IMPACT
      Users affected by this bug may get query results that do not match the query predicate.

      WORKAROUNDS
      None.

      RESOLUTION
      The patch fixes a logical bug where $elemMatch expressions were not correctly handled. Specifically, the result set needs to be filtered to verify that the document correctly matches all predicates of the $elemMatch document.

      AFFECTED VERSIONS
      MongoDB version 2.6.0 is affected by this bug.

      PATCHES
      The patch is included in the 2.6.1 production release.

      Original description

      Looks like in 2.6 there is a bug when a complex index exists and you use a range query in the first part of the index.

      I have tested this on 2.6.0 and 2.4.4. The bug does not exist on 2.4.4

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            nefiga Ben Rotz
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: