MCPcopy
hub / github.com/mongodb/node-mongodb-native / getMore

Method getMore

src/cursor/find_cursor.ts:101–145  ·  view source on GitHub ↗

@internal

()

Source from the content-addressed store, hash-verified

99
100 /** @internal */
101 override async getMore(): Promise<CursorResponse> {
102 const numReturned = this.numReturned;
103 const limit = this.findOptions.limit ?? Infinity;
104 const remaining = limit - numReturned;
105
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
115 try {
116 await this.close();
117 } catch (error) {
118 squashError(error);
119 }
120 return CursorResponse.emptyGetMore;
121 }
122
123 // TODO(DRIVERS-1448): Remove logic to enforce `limit` in the driver
124 let cleanup: () => void = noop;
125 const { batchSize } = this.cursorOptions;
126 if (batchSize != null && batchSize > remaining) {
127 this.cursorOptions.batchSize = remaining;
128
129 // After executing the final getMore, re-assign the batchSize back to its original value so that
130 // if the cursor is rewound and executed, the batchSize is still correct.
131 cleanup = () => {
132 this.cursorOptions.batchSize = batchSize;
133 };
134 }
135
136 try {
137 const response = await super.getMore();
138
139 this.numReturned = this.numReturned + response.batchSize;
140
141 return response;
142 } finally {
143 cleanup?.();
144 }
145 }
146
147 /**
148 * Get the count of documents for this cursor

Callers 1

fetchBatchFunction · 0.45

Calls 3

squashErrorFunction · 0.90
cleanupFunction · 0.70
closeMethod · 0.45

Tested by

no test coverage detected