diff --git a/db/query.cpp b/db/query.cpp index 0d5dd735a81..4cefff1755d 100644 --- a/db/query.cpp +++ b/db/query.cpp @@ -58,9 +58,24 @@ auto_ptr getIndexCursor(const char *ns, BSONObj& query, BSONObj& order, NamespaceDetails *d = nsdetails(ns); if( d == 0 ) return auto_ptr(); - // queryFields, e.g. { 'name' } - set queryFields; - query.getFieldNames(queryFields); + if( hint && !hint->empty() ) { + /* todo: more work needed. doesn't handle $lt & $gt for example. + waiting for query optimizer rewrite (see queryoptimizer.h) before finishing the work. + */ + for(int i = 0; i < d->nIndexes; i++ ) { + IndexDetails& ii = d->indexes[i]; + if( ii.indexName() == *hint ) { + BSONObj startKey = ii.getKeyFromQuery(query); + int direction = 1; + bool stopMiss = true; + if( simpleKeyMatch ) + *simpleKeyMatch = query.nFields() == startKey.nFields(); + if( isSorted ) *isSorted = false; + return auto_ptr( + new BtreeCursor(ii, startKey, direction, stopMiss)); + } + } + } if( !order.isEmpty() ) { set orderFields; @@ -87,24 +102,9 @@ auto_ptr getIndexCursor(const char *ns, BSONObj& query, BSONObj& order, } } - if( hint && !hint->empty() ) { - /* todo: more work needed. doesn't handle $lt & $gt for example. - waiting for query optimizer rewrite (see queryoptimizer.h) before finishing the work. - */ - for(int i = 0; i < d->nIndexes; i++ ) { - IndexDetails& ii = d->indexes[i]; - if( ii.indexName() == *hint ) { - BSONObj startKey = ii.getKeyFromQuery(query); - int direction = 1; - bool stopMiss = true; - if( simpleKeyMatch ) - *simpleKeyMatch = query.nFields() == startKey.nFields(); - if( isSorted ) *isSorted = false; - return auto_ptr( - new BtreeCursor(ii, startKey, direction, stopMiss)); - } - } - } + // queryFields, e.g. { 'name' } + set queryFields; + query.getFieldNames(queryFields); // regular query without order by for(int i = 0; i < d->nIndexes; i++ ) { @@ -736,7 +736,7 @@ QueryResult* runQuery(Message& message, const char *ns, int ntoskip, int _ntoret BSONObjBuilder builder; builder.append("cursor", c->toString()); builder.append("nscanned", nscanned); - builder.append("n", n); + builder.append("n", ordering ? so->size() : n); if( ordering ) builder.append("scanAndOrder", true); BSONObj obj = builder.done(); diff --git a/db/scanandorder.h b/db/scanandorder.h index f046b6cc925..a3f2a613313 100644 --- a/db/scanandorder.h +++ b/db/scanandorder.h @@ -98,6 +98,8 @@ public: dir = -1; } } + + int size() const { return best.size(); } void add(BSONObj o) { BSONObj k = order.getKeyFromObject(o);