Docs Menu
Docs Home
/ / /
Kotlin Sync Driver

Optimize Queries by Using Indexes

On this page

  • Overview
  • Sample Application
  • Single Field Index
  • Compound Index
  • Multikey Index
  • Geospatial Index
  • Unique Index
  • Wildcard Index
  • Clustered Index
  • Atlas Search Index Management
  • Create Search Index
  • List Search Indexes
  • Update Search Indexes
  • Delete Search Indexes
  • Text Index
  • Delete an Index

On this page, you can see copyable code examples that show how to manage different types of indexes by using the Kotlin Sync driver.

Tip

To learn more about working with indexes, see the Work with Indexes guide. To learn more about any of the indexes shown on this page, see the link 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}

The following example creates an ascending index on the specified field:

collection.createIndex(Indexes.ascending("<field name>"))

To learn more about single field indexes, see the Single-Field Indexes guide.

The following example creates a compound index on the specified fields:

collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>"))

To learn more about compound indexes, see the Compound Indexes guide.

The following example creates a multikey index on the specified array-valued field:

collection.createIndex(Indexes.ascending("<array field name>"))

The following example creates a 2dsphere index on the specified field that contains GeoJSON objects:

collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"))

The following example creates a unique index on the specified field:

val indexOptions = IndexOptions().unique(true)
collection.createIndex(Indexes.ascending("<field name>"), indexOptions)

The following example creates a wildcard index in the specified collection:

collection.createIndex(Indexes.ascending("$**"))

The following example creates a new collection with a clustered index on the _id field:

val clusteredIndexOptions = ClusteredIndexOptions(
Indexes.ascending("_id"),
true
)
val collection = database.createCollection("<collection name>", clusteredIndexOptions)

The following sections contain code examples that describe how to manage Atlas Search indexes.

To learn more about Atlas Search indexes, see the Atlas Search and Vector Search Indexes guide.

The following example creates an Atlas Search index on the specified field:

val index = Document("mappings", Document("dynamic", true))
collection.createSearchIndex("<index name>", index)

To learn more about creating serach indexes, see the Create a Search Index guide.

The following example prints a list of Atlas Search indexes in the specified collection:

val results = collection.listSearchIndexes()
results.forEach { result ->
println(result)
}

To learn more about listing search indexes, see the List Search Indexes guide.

The following example updates an existing Atlas Search index with the specified new index definition:

val newIndex = Document("mappings", Document("dynamic", true))
collection.updateSearchIndex("<index name>", newIndex)

To learn more about updating search indexes, see the Update a Search Index guide.

The following example deletes an Atlas Search index with the specified name:

collection.dropIndex("<index name>")

To learn more about deleting search indexes, see the Delete a Search Index guide.

The following example creates a text index on the specified string field:

collection.createIndex(Indexes.text("<field name>"))

The following example deletes an index with the specified name:

collection.dropIndex("<index name>")

Back

Monitor Data Changes