Skip to content

JvmMongoIterable

Implementation of MongoIterable based on MongoDB's FindFlow.

To access the inner iterable, see asKotlinMongoIterable.

To convert an existing MongoDB iterable into an instance of this class, see asKtMongo.

Functions

asFlow

open override fun asFlow(): Flow<Document>

Streams the results of this response into a Flow.

asKotlinMongoIterable

@LowLevelApi



fun asKotlinMongoIterable(): FindFlow<Document>

Converts a KtMongo MongoIterable into a MongoDB FindFlow.

asSequence

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, asStream (Java only) or the coroutines driver's asFlow

Replace with

<div class="highlight"><pre><code class="md-code__content"><span markdown>this.toList().asSequence()
</span></code></pre></div>

first

open suspend override fun first(): Document

Returns the first document found by this query, or throws an exception.

See also

Throws

NoSuchElementException

If this query returned no results.

firstOrNull

open suspend override fun firstOrNull(): Document?

Returns the first document found by this query, or null if none were found.

See also

forEach

open suspend override fun forEach(action: suspend (Document) -> Unit)

Executes action for each document returned by this query.

This method streams all returned elements into the action function. The entire response is not loaded at once into memory.

toList

open suspend override fun toList(): List<Document>

Reads the entirety of this response into a List.

Since lists are in-memory, this will load the entirety of the results of this query into memory.

See also

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, asStream (Java only) or the coroutines driver's asFlow

Replace with

<div class="highlight"><pre><code class="md-code__content"><span markdown>this.toList().asSequence()
</span></code></pre></div>

toSet

open suspend fun toSet(): Set<Document>

Reads the entirety of this response into a Set.

Since sets are in-memory, this will load the entirety of the results of this query into memory.

See also

toString

open override fun toString(): String