toSequence

Deprecated (with error)

Kotlin Sequences are not capable of closing a resource after they are done. Using sequences with a MongoIterable will create memory leaks. Instead, use toList, forEach, stream (Java only) or the coroutines driver's asFlow

Replace with

this.toList().asSequence()

This method always throws an exception.

Streaming a MongoIterable into a Sequence is not supported, because iterables must be closed, and sequences cannot detect when iteration finishes.

If you want to use a Sequence data structure for convenience, and the volume of data is low, use toList followed by asSequence. All data will be loaded in memory at once.

If streaming is important, either use forEach, asFlow or stream (Java-only).

See also

Execute an action for each result.

Store all results in a List.

Store all results in a Set.

Stream all results in a Flow.