Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversKotlin Coroutine

Find Multiple Documents

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:

  • A query filter that is passed to the find() method. The lt() filter matches only movies with a runtime of less than 15 minutes.

  • A sort that organizes returned documents in descending order by title ("Z" before "A").

  • A projection that includes the objects in the title and imdb fields and excludes the _id field using the helper method excludeId().

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.lt
import com.mongodb.client.model.Projections
import com.mongodb.client.model.Sorts
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
data 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 {
// Replace the uri string with your MongoDB deployment's connection string
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:

  • FindFlow

  • find()

←  Find a DocumentInsert Operations →