[SERVER-5740] index bounds may be incorrectly determined for $not:$elemMatch queries Created: 01/May/12  Updated: 11/Jul/16  Resolved: 09/Jun/12

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 2.1.2

Type: Bug Priority: Major - P3
Reporter: Aaron Staple Assignee: Aaron Staple
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

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' ] ) );



 Comments   
Comment by auto [ 09/Jun/12 ]

Author:

{u'login': u'astaple', u'name': u'Aaron', u'email': u'aaron@10gen.com'}

Message: SERVER-4180 Fixes and cleanups for $elemMatch index range calculation, in particular to clean up incorrect bound generation for SERVER-1264 style $elemMatch queries. Includes fixes for SERVER-5740 and SERVER-5741.
Branch: master
https://github.com/mongodb/mongo/commit/2b572b9c07d8d3feda949153e20b9eee8706ff7d

Generated at Thu Feb 08 03:09:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.