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

index bounds may be incorrectly determined for $not:$elemMatch queries

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

      Currently the index bounds for a query like { a:{ $not:{ $elemMatch:{ b:

      { $ne:2 }

      , c:3 } } } } are determined by applying $not to $ne rather than to $elemMatch. This is inconsistent with the matcher's behavior.

      Test:

      // Check index bound determination for $not:$elemMatch queries.
      
      t = db.jstests_arrayfind6;
      t.drop();
      
      t.save( { a:{ b:1, c:2 } } );
      
      function checkElemMatchMatches() {
          assert.eq( 1, t.count( { a:{ $elemMatch:{ b:1, c:2 } } } ) );
          assert.eq( 0, t.count( { a:{ $not:{ $elemMatch:{ b:1, c:2 } } } } ) );
          assert.eq( 1, t.count( { a:{ $not:{ $elemMatch:{ b:1, c:3 } } } } ) );
          assert.eq( 1, t.count( { a:{ $not:{ $elemMatch:{ b:{ $ne:1 }, c:3 } } } } ) );
          // Index bounds must be determined for $not:$elemMatch, not $not:$ne.  In this case if index
          // bounds are determined for $not:$ne, the a.b index will be constrained to the interval [2,2]
          // and the saved document will not be matched as it should.
          assert.eq( 1, t.count( { a:{ $not:{ $elemMatch:{ b:{ $ne:2 }, c:3 } } } } ) );
      }
      
      checkElemMatchMatches();
      t.ensureIndex( { 'a.b':1 } );
      checkElemMatchMatches();
      
      // Verify index bounds - see comment above for the $ne:2 case.
      assert( !friendlyEqual( [ [ 2, 2 ] ],
                             t.find( { a:{ $not:{ $elemMatch:{ b:{ $ne:2 }, c:3 } } } } ).explain()
                             .indexBounds[ 'a.b' ] ) );
      

            Assignee:
            aaron Aaron Staple
            Reporter:
            aaron Aaron Staple
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: