Skip to content

Commit 81de3cd

Browse files
docs: add note to cursor.map that null is not supported
1 parent 9894419 commit 81de3cd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/cursor/abstract_cursor.ts

+23
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,29 @@ export abstract class AbstractCursor<
493493
* this function's transform.
494494
*
495495
* @remarks
496+
*
497+
* **Note** Cursors use `null` internally to indicate that there are no more documents in the cursor. Providing a mapping
498+
* function that maps values to `null` will result in the cursor closing itself before it has finished iterating
499+
* all documents. This will **not** result in a memory leak, just surprising behavior. For example:
500+
*
501+
* ```typescript
502+
* const cursor = collection.find({});
503+
* cursor.map(() => null);
504+
*
505+
* const documents = await cursor.toArray();
506+
* // documents is always [], regardless of how many documents are in the collection.
507+
* ```
508+
*
509+
* Other falsey values are allowed:
510+
*
511+
* ```typescript
512+
* const cursor = collection.find({});
513+
* cursor.map(() => '');
514+
*
515+
* const documents = await cursor.toArray();
516+
* // documents is now an array of empty strings
517+
* ```
518+
*
496519
* **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor,
497520
* it **does not** return a new instance of a cursor. This means when calling map,
498521
* you should always assign the result to a new variable in order to get a correctly typed cursor variable.

0 commit comments

Comments
 (0)