diff --git a/db/queryoptimizer.cpp b/db/queryoptimizer.cpp index 6f33e1ab080..a04a3d749a9 100644 --- a/db/queryoptimizer.cpp +++ b/db/queryoptimizer.cpp @@ -120,7 +120,8 @@ namespace mongo { optimal_ = true; if ( exactIndexedQueryCount == fbs.nNontrivialBounds() && orderFieldsUnindexed.size() == 0 && - exactIndexedQueryCount == index->keyPattern().nFields() ) { + exactIndexedQueryCount == index->keyPattern().nFields() && + exactIndexedQueryCount == fbs.query().nFields() ) { exactKeyMatch_ = true; } if ( startKey_.isEmpty() ) diff --git a/dbtests/queryoptimizertests.cpp b/dbtests/queryoptimizertests.cpp index a423b0a6687..5087b36ec66 100644 --- a/dbtests/queryoptimizertests.cpp +++ b/dbtests/queryoptimizertests.cpp @@ -464,6 +464,14 @@ namespace QueryOptimizerTests { } }; + class MoreKeyMatch : public Base { + public: + void run() { + QueryPlan p( FBS( BSON( "a" << "r" << "b" << NE << "q" ) ), BSON( "a" << 1 ), INDEX( "a" << 1 ) ); + ASSERT( !p.exactKeyMatch() ); + } + }; + class ExactKeyQueryTypes : public Base { public: void run() { @@ -980,6 +988,7 @@ namespace QueryOptimizerTests { add< QueryPlanTests::Optimal >(); add< QueryPlanTests::MoreOptimal >(); add< QueryPlanTests::KeyMatch >(); + add< QueryPlanTests::MoreKeyMatch >(); add< QueryPlanTests::ExactKeyQueryTypes >(); add< QueryPlanTests::Unhelpful >(); add< QueryPlanSetTests::NoIndexes >(); diff --git a/jstests/count.js b/jstests/count.js index 129a369f973..f862aec2c84 100644 --- a/jstests/count.js +++ b/jstests/count.js @@ -21,3 +21,4 @@ t.save( {a:true,b:false} ); t.ensureIndex( {b:1,a:1,c:1} ); assert.eq( 1, t.find( {a:true,b:false} ).count() ); assert.eq( 1, t.find( {b:false,a:true} ).count() ); +