@@ -103,19 +103,19 @@ export class FindCursor<TSchema = any> extends ExplainableCursor<TSchema> {
103103 const limit = this . findOptions . limit ?? Infinity ;
104104 const remaining = limit - numReturned ;
105105
106- if ( numReturned >= limit ) {
106+ if ( numReturned === limit && ! this . id ?. isZero ( ) ) {
107+ // this is an optimization for the special case of a limit for a find command to avoid an
108+ // extra getMore when the limit has been reached and the limit is a multiple of the batchSize.
109+ // This is a consequence of the new query engine in 5.0 having no knowledge of the limit as it
110+ // produces results for the find command. Once a batch is filled up, it is returned and only
111+ // on the subsequent getMore will the query framework consider the limit, determine the cursor
112+ // is exhausted and return a cursorId of zero.
113+ // instead, if we determine there are no more documents to request from the server, we preemptively
114+ // close the cursor
107115 try {
108116 await this . close ( ) ;
109117 } catch ( error ) {
110118 squashError ( error ) ;
111- // this is an optimization for the special case of a limit for a find command to avoid an
112- // extra getMore when the limit has been reached and the limit is a multiple of the batchSize.
113- // This is a consequence of the new query engine in 5.0 having no knowledge of the limit as it
114- // produces results for the find command. Once a batch is filled up, it is returned and only
115- // on the subsequent getMore will the query framework consider the limit, determine the cursor
116- // is exhausted and return a cursorId of zero.
117- // instead, if we determine there are no more documents to request from the server, we preemptively
118- // close the cursor
119119 }
120120 return CursorResponse . emptyGetMore ;
121121 }
0 commit comments