Docs Menu
Docs Home
/ / /
Kotlin Sync Driver

Read Data from MongoDB

On this page

  • Overview
  • Sample Application
  • Find Documents
  • Count Documents in a Collection
  • Count Documents Returned from a Query
  • Estimated Document Count
  • Retrieve Distinct Values
  • Monitor Data Changes

On this page, you can see copyable code examples that show common methods you can use to retrieve documents by using the Kotlin Sync driver.

Tip

To learn more about any of the methods shown on this page, see the link to a relevant guide provided in each section.

To use an example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders in the code examples, such as <connection string URI>, with the relevant values for your MongoDB deployment.

You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:

  1. Ensure you have the Kotlin Sync driver installed in your Maven or Gradle project.

  2. Copy the following code and paste it into a new .kt file.

  3. Copy a code example from this page and paste it on the specified lines in the file.

1import com.mongodb.ConnectionString
2import com.mongodb.MongoClientSettings
3import com.mongodb.client.model.*
4import com.mongodb.kotlin.client.*
5import org.bson.Document
6
7fun main() {
8 val uri = "<connection string URI>"
9
10 val settings = MongoClientSettings.builder()
11 .applyConnectionString(ConnectionString(uri))
12 .retryWrites(true)
13 .build()
14
15 // Create a new client and connect to the server
16 val mongoClient = MongoClient.create(settings)
17 val database = mongoClient.getDatabase("<database name>")
18 val collection = database.getCollection<Document>("<collection name>")
19
20 // Start example code here
21
22 // End example code here
23}

Tip

For instructions about how to install the Kotlin Sync driver, see Download and Install.

The following example retrieves a list of documents that match the criteria specified by the given filter:

val filter = <filter>
val results = collection.find(filter)
results.forEach { result ->
print(result)
}

To learn more about the find() method, see the Find Documents guide.

The following example returns the number of documents in the specified collection:

val count = collection.countDocuments()
print(count)

To learn more about the countDocuments() method, see the Retrieve an Accurate Count section of the Count Documents guide.

The following example returns the number of documents that match the criteria specified by the given filter:

val filter = <filter>
val queryCount = collection.countDocuments(filter)
print(queryCount)

To learn more about the countDocuments() method, see the Retrieve an Accurate Count section of the Count Documents guide.

The following example returns an approximate number of documents in the specified collection based on collection metadata:

val estimatedCount = collection.estimatedDocumentCount()
print(estimatedCount)

To learn more about the estimatedDocumentCount() method, see the Retrieve an Estimated Count section of the Count Documents guide.

The following example returns all distinct values of the specified field name in a given collection:

val distinctResults = collection.distinct("<field name>")
distinctResults.forEach { result ->
print(result)
}

To learn more about the distinct() method, see the Retrieve Distinct Field Values guide.

The following example creates a change stream for a given collection and prints out subsequent change events in that collection:

val changeStream = collection.watch()
changeStream.forEach { changeEvent ->
print(changeEvent)
}

To learn more about the watch() method, see the Monitor Data Changes guide.

Back

Transactions