A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/languages/kotlin/kotlin-sync-driver/current/read/ below:

Read Data - Kotlin Sync Driver

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.ConnectionString2import com.mongodb.MongoClientSettings3import com.mongodb.client.model.*4import com.mongodb.kotlin.client.*5import org.bson.Document67fun main() {8    val uri = "<connection string URI>"9    10    val settings = MongoClientSettings.builder()11        .applyConnectionString(ConnectionString(uri))12        .retryWrites(true)13        .build()1415    16    val mongoClient = MongoClient.create(settings)17    val database = mongoClient.getDatabase("<database name>")18    val collection = database.getCollection<Document>("<collection name>")1920    2122    23}

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.


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