Skip to main content

CursorStream

CursorStream adapts a Cursor into a standard Node.js Readable stream, so query results can be piped directly (e.g. to an HTTP response) instead of being iterated manually. See Cursors & Streaming for the full usage guide.

CursorStream extends stream.Readable.

Constructor

new CursorStream(cursor: Cursor, options?: CursorStreamOptions)

You don't normally construct a CursorStream yourself — use cursor.toStream(options?) instead.

interface CursorStreamOptions {
objectMode?: boolean;
limit?: number;
}
OptionTypeDefaultDescription
objectModebooleanfalseEmit one row object per data event instead of JSON text chunks.
limitnumberunlimitedStop the stream after this many rows.

Properties

PropertyTypeDescription
isClosedbooleanMirrors the underlying cursor's isClosed.

Methods

close()

close(): Promise<void>

Pauses and unpipes the stream, then closes the underlying cursor (which, if the cursor was retaining its connection, releases it too).

const stream = cursor.toStream({ objectMode: true });
stream.on('data', row => console.log(row));
stream.on('end', () => console.log('done'));

In non-object mode, the stream emits text chunks that together form a single JSON array: [, then each row as JSON.stringify(row) separated by commas, then ] — suitable for piping straight through to an HTTP response as a JSON array body.