lib/mongodb/mongodb/lib/operations/cursor_ops.js
function toArray(cursor, callback) {
  const Cursor = require('../cursor');

  const items = [];

  // Reset cursor
  cursor.rewind();
  cursor.s.state = Cursor.INIT;

  // Fetch all the documents
  const fetchDocs = () => {
    cursor._next((err, doc) => {
      if (err) {
        return cursor._endSession
          ? cursor._endSession(() => handleCallback(callback, err))
          : handleCallback(callback, err);
      }
      if (doc == null) {
        return cursor.close({ skipKillCursors: true }, () => handleCallback(callback, null, items));
      }

      // Add doc to items
      items.push(doc);

      // Get all buffered objects
      if (cursor.bufferedCount() > 0) {
        let docs = cursor.readBufferedDocuments(cursor.bufferedCount());

        // Transform the doc if transform method added
        if (cursor.s.transforms && typeof cursor.s.transforms.doc === 'function') {
          docs = docs.map(cursor.s.transforms.doc);
        }

        push.apply(items, docs);
      }

      // Attempt a fetch
      fetchDocs();
    });
  };

  fetchDocs();
}
function toArray(cursor, callback) {
  const Cursor = require('../cursor');

  const items = [];

  // Reset cursor
  cursor.rewind();
  cursor.s.state = Cursor.INIT;

  // Fetch all the documents
  const fetchDocs = () => {
    cursor._next((err, doc) => {
      if (err) {
        return cursor._endSession
          ? cursor._endSession(() => handleCallback(callback, err))
          : handleCallback(callback, err);
      }
      if (doc == null) {
        return cursor.close({ skipKillCursors: true }, () => handleCallback(callback, null, items));
      }

      // Add doc to items
      items.push(doc);

      // Get all buffered objects
      if (cursor.bufferedCount() > 0) {
        let docs = cursor.readBufferedDocuments(cursor.bufferedCount());

        // Transform the doc if transform method added
        if (cursor.s.transforms && typeof cursor.s.transforms.doc === 'function') {
          docs = docs.map(cursor.s.transforms.doc);
        }

        push.apply(items, docs);
      }

      // Attempt a fetch
      fetchDocs();
    });
  };

  fetchDocs();
}
function toArray ( cursor , callback ) { const Cursor = require ( '../cursor' ) ; const items = [ ] ; // Reset cursor cursor . rewind ( ) ; cursor . s . state = Cursor . INIT ; // Fetch all the documents const fetchDocs = ( ) => { cursor . _next ( ( err , doc ) => { if ( err ) { return cursor . _endSession ? cursor . _endSession ( ( ) => handleCallback ( callback , err ) ) : handleCallback ( callback , err ) ; } if ( doc == null ) { return cursor . close ( { skipKillCursors : true } , ( ) => handleCallback ( callback , null , items ) ) ; } // Add doc to items items . push ( doc ) ; // Get all buffered objects if ( cursor . bufferedCount ( ) > 0 ) { let docs = cursor . readBufferedDocuments ( cursor . bufferedCount ( ) ) ; // Transform the doc if transform method added if ( cursor . s . transforms && typeof cursor . s . transforms . doc === 'function' ) { docs = docs . map ( cursor . s . transforms . doc ) ; } push . apply ( items , docs ) ; } // Attempt a fetch fetchDocs ( ) ; } ) ; } ; fetchDocs ( ) ; }
Returns an array of documents. See Cursor.prototype.toArray for more information.

@method
@param {Cursor} cursor The Cursor instance from which to get the next document.
@param {Cursor~toArrayResultCallback} [callback] The result callback.
Returns an array of documents . See Cursor . prototype . toArray for more information .