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

Function toArray

src/cursor/abstract_cursor.ts:654–676  ·  view source on GitHub ↗

* Returns an array of documents. The caller is responsible for making sure that there * is enough memory to store the results. Note that the array only contains partial * results when this cursor had been previously accessed. In that case, * cursor.rewind() can be used to reset the cursor.

()

Source from the content-addressed store, hash-verified

652 * cursor.rewind() can be used to reset the cursor.
653 */
654 async toArray(): Promise<TSchema[]> {
655 this.signal?.throwIfAborted();
656
657 const array: TSchema[] = [];
658 // at the end of the loop (since readBufferedDocuments is called) the buffer will be empty
659 // then, the 'await of' syntax will run a getMore call
660 for await (const document of this) {
661 array.push(document);
662 const docs = this.readBufferedDocuments();
663 if (this.transform != null) {
664 for (const doc of docs) {
665 array.push(await this.transformDocument(doc));
666 }
667 } else {
668 // Note: previous versions of this logic used `array.push(...)`, which adds each item
669 // to the callstack. For large arrays, this can exceed the maximum call size.
670 for (const doc of docs) {
671 array.push(doc);
672 }
673 }
674 }
675 return array;
676 }
677 /**
678 * Add a cursor flag to the cursor
679 *

Callers

nothing calls this directly

Calls 2

throwIfAbortedMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected