Docs Menu
Docs Home
/ / /
Kotlin Sync Driver
/

Retrieve Data

On this page

  • Overview
  • Sample Data
  • Find Documents
  • Find Documents Example
  • Modify Find Behavior
  • Additional Information
  • API Documentation

In this guide, you can learn how to use the Kotlin Sync driver to retrieve data from a MongoDB collection by using read operations. You can call the find() method to retrieve documents that match a set of criteria specified in a query filter.

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

The documents in this collection are modeled by the following Kotlin data class:

data class Restaurant(
val name: String,
val cuisine: String
)

The find() method retrieves documents from a collection. This method takes a query filter and returns all matching documents. A query filter is a document that specifies the criteria that the driver uses to match documents from the collection.

To learn more about query filters, see the Specify a Query guide.

The following example uses the find() method to find all documents in which the value of the cuisine field is "Spanish":

val results = collection.find(eq(Restaurant::cuisine.name, "Spanish"))

The find() operation in the preceding example returns a FindIterable object, which you can iterate through by using the forEach() method, as shown in the following example:

val results = collection.find(eq(Restaurant::cuisine.name, "Spanish"))
results.forEach { result ->
println(result)
}
Restaurant(name=Tropicoso Club, cuisine=Spanish)
Restaurant(name=Beso, cuisine=Spanish)
Restaurant(name=Sabor Latino Restaurant, cuisine=Spanish)
...

Note

Find All Documents

To find all documents in a collection, pass an empty filter to the find() method:

val results = collection.find()

You can modify the behavior of the find() method by chaining methods to the find() method call. The following table describes commonly used methods used for modifying queries:

Method
Description
batchSize()
Limits the number of documents to return per batch. To learn more about batch size, see cursor.batchSize() in the MongoDB Server manual.
collation()
Sets the collation options for the query.
comment()
Specifies a string to attach to the query. This can help you trace and interpret the operation in the server logs and in profile data. To learn more about query comments, see $comment in the MongoDB Server manual.
hint()
Specifies the index to use for the query.
limit()
Limits the number of documents to be returned from the query.
maxTime()
Sets the maximum execution time on the server for this operation.
skip()
Sets the number of documents to skip.
sort()
Defines the sort criteria to apply to the query.

The following example chains the limit() and maxTime() methods to limit the number of documents returned by the query to 10 and set a maximum execution time of 10000 milliseconds on the operation:

val results = collection
.find(eq(Restaurant::cuisine.name, "Spanish"))
.limit(10)
.maxTime(10000)

For a full list of methods that modify the behavior of find(), see the API documentation for the FindIterable class.

To learn more about query filters, see Specify a Query.

To view runnable code examples that retrieve documents by using the Kotlin Sync driver, see Read Data from MongoDB.

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

  • find()

  • FindIterable

Back

Specify a Query