@@ -77,28 +77,29 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
77
77
78
78
const response = await executeOperation ( this . client , findOperation ) ;
79
79
80
- // TODO: We only need this for legacy queries that do not support `limit`, maybe
81
- // the value should only be saved in those cases.
82
- if ( response . cursor ) {
83
- this [ kNumReturned ] = response . cursor . firstBatch . length ;
84
- } else {
85
- this [ kNumReturned ] = response . documents ? response . documents . length : 0 ;
86
- }
80
+ this [ kNumReturned ] = response . cursor . firstBatch . length ;
87
81
88
82
// TODO: NODE-2882
89
83
return { server : findOperation . server , session, response } ;
90
84
}
91
85
92
86
/** @internal */
93
87
override async getMore ( batchSize : number ) : Promise < Document | null > {
94
- // NOTE: this is to support client provided limits in pre-command servers
95
88
const numReturned = this [ kNumReturned ] ;
96
89
if ( numReturned ) {
97
90
const limit = this [ kBuiltOptions ] . limit ;
98
91
batchSize =
99
92
limit && limit > 0 && numReturned + batchSize > limit ? limit - numReturned : batchSize ;
100
93
101
94
if ( batchSize <= 0 ) {
95
+ // this is an optimization for the special case of a limit for a find command to avoid an
96
+ // extra getMore when the limit has been reached and the limit is a multiple of the batchSize.
97
+ // This is a consequence of the new query engine in 5.0 having no knowledge of the limit as it
98
+ // produces results for the find command. Once a batch is filled up, it is returned and only
99
+ // on the subsequent getMore will the query framework consider the limit, determine the cursor
100
+ // is exhausted and return a cursorId of zero.
101
+ // instead, if we determine there are no more documents to request from the server, we preemptively
102
+ // close the cursor
102
103
await this . close ( ) . catch ( ( ) => null ) ;
103
104
return { cursor : { id : Long . ZERO , nextBatch : [ ] } } ;
104
105
}
0 commit comments