diff --git a/db/queryoptimizer.cpp b/db/queryoptimizer.cpp index 466527148f7..9d51623504a 100644 --- a/db/queryoptimizer.cpp +++ b/db/queryoptimizer.cpp @@ -201,11 +201,11 @@ namespace mongo { uassert( "bad hint", false ); } - BSONObj bestIndex = indexForPattern( ns, fbs_.pattern() ); + BSONObj bestIndex = indexForPattern( ns, fbs_.pattern( order_ ) ); if ( !bestIndex.isEmpty() ) { usingPrerecordedPlan_ = true; mayRecordPlan_ = false; - oldNScanned_ = nScannedForPattern( ns, fbs_.pattern() ); + oldNScanned_ = nScannedForPattern( ns, fbs_.pattern( order_ ) ); if ( !strcmp( bestIndex.firstElement().fieldName(), "$natural" ) ) { // Table scan plan plans_.push_back( PlanPtr( new QueryPlan( fbs_, order_ ) ) ); @@ -262,7 +262,7 @@ namespace mongo { // plans_.size() > 1 if addOtherPlans was called in Runner::run(). if ( res->complete() || plans_.size() > 1 ) return res; - registerIndexForPattern( fbs_.ns(), fbs_.pattern(), BSONObj(), 0 ); + registerIndexForPattern( fbs_.ns(), fbs_.pattern( order_ ), BSONObj(), 0 ); init(); } Runner r( *this, op ); diff --git a/db/queryoptimizer.h b/db/queryoptimizer.h index 73cd262d628..cfb584472a3 100644 --- a/db/queryoptimizer.h +++ b/db/queryoptimizer.h @@ -51,7 +51,7 @@ namespace mongo { BSONObj query() const { return fbs_.query(); } const FieldBound &bound( const char *fieldName ) const { return fbs_.bound( fieldName ); } void registerSelf( int nScanned ) const { - registerIndexForPattern( ns(), fbs_.pattern(), indexKey(), nScanned ); + registerIndexForPattern( ns(), fbs_.pattern( order_ ), indexKey(), nScanned ); } private: const FieldBoundSet &fbs_; diff --git a/db/queryutil.h b/db/queryutil.h index 5db0db9ecb7..1441ba85d11 100644 --- a/db/queryutil.h +++ b/db/queryutil.h @@ -87,8 +87,6 @@ namespace mongo { } if ( j != other.fieldTypes_.end() ) return true; - out() << "sort_: " << sort_ << endl; - out() << "other.sort_: " << other.sort_ << endl; return sort_.woCompare( other.sort_ ) < 0; } private: @@ -97,6 +95,8 @@ namespace mongo { sort_ = normalizeSort( sort ); } BSONObj static normalizeSort( const BSONObj &spec ) { + if ( spec.isEmpty() ) + return spec; int direction = ( spec.firstElement().number() >= 0 ) ? 1 : -1; BSONObjIterator i( spec ); BSONObjBuilder b; diff --git a/dbtests/queryoptimizertests.cpp b/dbtests/queryoptimizertests.cpp index 69a63ef6ad6..d811fe59223 100644 --- a/dbtests/queryoptimizertests.cpp +++ b/dbtests/queryoptimizertests.cpp @@ -751,6 +751,11 @@ namespace QueryOptimizerTests { TestOp newOriginal; s2.runOp( newOriginal ); nPlans( 3 ); + + QueryPlanSet s3( ns(), BSON( "a" << 4 ), BSON( "b" << 1 << "c" << 1 ) ); + TestOp newerOriginal; + s3.runOp( newerOriginal ); + nPlans( 3 ); runQuery(); nPlans( 1 ); @@ -790,9 +795,9 @@ namespace QueryOptimizerTests { QueryPlanSet s( ns(), BSON( "a" << 4 ), BSON( "b" << 1 ) ); ScanOnlyTestOp op; s.runOp( op ); - ASSERT( fromjson( "{$natural:1}" ).woCompare( indexForPattern( ns(), s.fbs().pattern() ) ) == 0 ); + ASSERT( fromjson( "{$natural:1}" ).woCompare( indexForPattern( ns(), s.fbs().pattern( BSON( "b" << 1 ) ) ) ) == 0 ); - QueryPlanSet s2( ns(), BSON( "a" << 4 ), emptyObj ); + QueryPlanSet s2( ns(), BSON( "a" << 4 ), BSON( "b" << 1 ) ); TestOp op2; ASSERT( s2.runOp( op2 )->complete() ); }