Skip to content

Commit

Permalink
Select index if query contains condition for first index field
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Feb 12, 2009
1 parent bb13742 commit cd2a542
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 1 addition & 8 deletions db/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,8 @@ namespace mongo {
for (int i = 0; i < d->nIndexes; i++ ) {
BSONObj idxInfo = d->indexes[i].info.obj(); // { name:, ns:, key: }
BSONObj idxKey = idxInfo.getObjectField("key");
set<string> keyFields;
idxKey.getFieldNames(keyFields);

bool subset = true;
for( set<string>::iterator j = queryFields.begin(); subset && j != queryFields.end(); ++j )
if ( keyFields.count( *j ) == 0 )
subset = false;

if ( subset ) {
if ( queryFields.count( idxKey.firstElement().fieldName() ) > 0 ) {
BSONObj q = query.extractFieldsUnDotted(idxKey);
assert(q.objsize() != 0); // guard against a seg fault if details is 0

Expand Down
6 changes: 6 additions & 0 deletions jstests/index7.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ noIndex( f.find().sort( { b: 1, a: 1 } ) );
noIndex( f.find() );
noIndex( f.find( { c: 1 } ) );
index( f.find( { a: 1 } ) );
assert.eq( 1, f.find( { a: 1 } ).explain().startKey.a );
assert.eq( 1, f.find( { a: 1 } ).explain().endKey.a );
index( f.find( { a: 1, c: 1 } ) );
assert.eq( 1, f.find( { a: 1, c: 1 } ).explain().startKey.a );
assert.eq( 1, f.find( { a: 1, c: 1 } ).explain().endKey.a );
noIndex( f.find( { b: 1 } ) );
index( f.find( { a: 1, b: 1 } ) );
index( f.find( { b: 1, a: 1 } ) );

Expand Down

0 comments on commit cd2a542

Please sign in to comment.