@@ -98,34 +98,29 @@ export class FindCursor<TSchema = any> extends ExplainableCursor<TSchema> {
9898 }
9999
100100 /** @internal */
101- override async getMore ( batchSize : number ) : Promise < CursorResponse > {
101+ override async getMore ( ) : Promise < CursorResponse > {
102102 const numReturned = this . numReturned ;
103- if ( numReturned ) {
104- // TODO(DRIVERS-1448): Remove logic to enforce `limit` in the driver
105- const limit = this . findOptions . limit ;
106- batchSize =
107- limit && limit > 0 && numReturned + batchSize > limit ? limit - numReturned : batchSize ;
108-
109- if ( batchSize <= 0 ) {
110- try {
111- await this . close ( ) ;
112- } catch ( error ) {
113- squashError ( error ) ;
114- // this is an optimization for the special case of a limit for a find command to avoid an
115- // extra getMore when the limit has been reached and the limit is a multiple of the batchSize.
116- // This is a consequence of the new query engine in 5.0 having no knowledge of the limit as it
117- // produces results for the find command. Once a batch is filled up, it is returned and only
118- // on the subsequent getMore will the query framework consider the limit, determine the cursor
119- // is exhausted and return a cursorId of zero.
120- // instead, if we determine there are no more documents to request from the server, we preemptively
121- // close the cursor
122- }
123- return CursorResponse . emptyGetMore ;
103+ const limit = this . findOptions . limit ;
104+
105+ if ( numReturned && limit && numReturned >= limit ) {
106+ try {
107+ await this . close ( ) ;
108+ } catch ( error ) {
109+ squashError ( error ) ;
110+ // this is an optimization for the special case of a limit for a find command to avoid an
111+ // extra getMore when the limit has been reached and the limit is a multiple of the batchSize.
112+ // This is a consequence of the new query engine in 5.0 having no knowledge of the limit as it
113+ // produces results for the find command. Once a batch is filled up, it is returned and only
114+ // on the subsequent getMore will the query framework consider the limit, determine the cursor
115+ // is exhausted and return a cursorId of zero.
116+ // instead, if we determine there are no more documents to request from the server, we preemptively
117+ // close the cursor
124118 }
119+ return CursorResponse . emptyGetMore ;
125120 }
126121
127- const response = await super . getMore ( batchSize ) ;
128- // TODO: wrap this in some logic to prevent it from happening if we don't need this support
122+ const response = await super . getMore ( ) ;
123+
129124 this . numReturned = this . numReturned + response . batchSize ;
130125
131126 return response ;
0 commit comments