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/findOne/ below:

Find a Document - Kotlin Coroutine

You can retrieve a single document in a collection by chaining together the find() and first() methods on a MongoCollection object. You can 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 other methods to the find() method such as sort() which organizes the matched documents in a specified order, and projection() which configures the fields included 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, such as first() and firstOrNull(). The firstOrNull() method returns the first document from the retrieved results or null if there are no results. The first() method returns the first document or throws a NoSuchElementException exception if no documents match the query.

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 a single document from 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.eqimport com.mongodb.client.model.Filters.ltimport com.mongodb.client.model.Projectionsimport com.mongodb.client.model.Sortsimport com.mongodb.kotlin.client.coroutine.MongoClientimport kotlinx.coroutines.flow.firstOrNullimport kotlinx.coroutines.runBlockingimport usageExamples.find.Resultsdata class Movie(val title: String, val runtime: Int, val imdb: IMDB) {    data class IMDB(val rating: Double)}data class Results(val title: String, val imdb: Movie.IMDB)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(eq(Movie::title.name, "The Room"))        .projection(projectionFields)        .sort(Sorts.descending("${Movie::imdb.name}.${Movie.IMDB::rating.name}"))        .firstOrNull()    if (resultsFlow == null) {        println("No results found.");    } else {        println(resultsFlow)    }    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