summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron <aaron@10gen.com>2009-02-12 10:11:02 -0500
committerAaron <aaron@10gen.com>2009-02-12 10:11:02 -0500
commitcd2a54233766f21d1965442bfd99eab5078a9d85 (patch)
treecb38928839e2f2d2693117dcab834c029df64f98
parentbb137426635a0c79cd7ee4407ea9316908114e9c (diff)
downloadmongo-cd2a54233766f21d1965442bfd99eab5078a9d85.tar.gz
Select index if query contains condition for first index fieldr0.2.1
-rw-r--r--db/query.cpp9
-rw-r--r--jstests/index7.js6
2 files changed, 7 insertions, 8 deletions
diff --git a/db/query.cpp b/db/query.cpp
index 5195dea984a..0bb52368554 100644
--- a/db/query.cpp
+++ b/db/query.cpp
@@ -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
diff --git a/jstests/index7.js b/jstests/index7.js
index bfbe9c8e07f..b5a308e79db 100644
--- a/jstests/index7.js
+++ b/jstests/index7.js
@@ -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 } ) );