Read Data from MongoDB
On this page
Overview
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.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the Kotlin Sync driver installed in your Maven or Gradle project.
Copy the following code and paste it into a new
.kt
file.Copy a code example from this page and paste it on the specified lines in the file.
1 import com.mongodb.ConnectionString 2 import com.mongodb.MongoClientSettings 3 import com.mongodb.client.model.* 4 import com.mongodb.kotlin.client.* 5 import org.bson.Document 6 7 fun 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.
Find Documents
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.
Count Documents in a Collection
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.
Count Documents Returned from a Query
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.
Estimated Document Count
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.
Retrieve Distinct Values
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.
Monitor Data Changes
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.