A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/drivers/kotlin/coroutine/current/usage-examples/find/ below:

Find Multiple Documents - Kotlin Coroutine

You can query for multiple documents in a collection by calling the find() method on a MongoCollection object. Pass a query filter to the find() method to query for and return documents that match the filter in the collection. If you do not include a filter, MongoDB returns all the documents in the collection.

For more information on querying MongoDB with the Kotlin driver, see our guide on Querying Documents.

You can also chain methods to the find() method such as sort() which organizes the matched documents in a specified order and projection() which configures the included fields in the returned documents.

For more information on the sort() method, see our guide on Sorting. For more information on the projection() method, see our guide on Projections

The find() method returns an instance of FindFlow, a class that offers several methods to access, organize, and traverse the results.

FindFlow also obtains methods from its delegate interface Flow from the Kotlin Coroutines library. You can call the collect() method to iterate through the fetched results. You can also call terminal methods, such as firstOrNull() to return either the first document or null if there are no results, or first() to return the first document in the collection. If no documents match the query, calling first() throws a NoSuchElementException exception.

For more information on accessing data from a flow with the Kotlin driver, see our guide on Accessing Data From a Flow.

The following snippet finds and prints all documents that match a query on the movies collection. It uses the following objects and methods:

Note

This example connects to an instance of MongoDB using a connection URI. To learn more about connecting to your MongoDB instance, see the connection guide.

import com.mongodb.client.model.Filters.ltimport com.mongodb.client.model.Projectionsimport com.mongodb.client.model.Sortsimport com.mongodb.kotlin.client.coroutine.MongoClientimport kotlinx.coroutines.runBlockingdata class Movie(val title: String, val runtime: Int, val imdb: IMDB){    data class IMDB(val rating: Double)}data class Results(val title: String)fun main() = runBlocking {        val uri = "<connection string uri>"    val mongoClient = MongoClient.create(uri)    val database = mongoClient.getDatabase("sample_mflix")    val collection = database.getCollection<Movie>("movies")    val projectionFields= Projections.fields(        Projections.include(Movie::title.name, Movie::imdb.name),        Projections.excludeId()    )    val resultsFlow = collection.withDocumentClass<Results>()        .find(lt(Movie::runtime.name, 15))        .projection(projectionFields)        .sort(Sorts.descending(Movie::title.name))    resultsFlow.collect { println(it) }    mongoClient.close()}

For additional information on the classes and methods mentioned on this page, see the following API Documentation:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4