Optimize Queries by Using Indexes
On this page
Overview
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.
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 }
Single Field Index
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.
Compound Index
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.
Multikey Index
The following example creates a multikey index on the specified array-valued field:
collection.createIndex(Indexes.ascending("<array field name>"))
Geospatial Index
The following example creates a 2dsphere index on the specified field that contains GeoJSON objects:
collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>"))
Unique Index
The following example creates a unique index on the specified field:
val indexOptions = IndexOptions().unique(true) collection.createIndex(Indexes.ascending("<field name>"), indexOptions)
Wildcard Index
The following example creates a wildcard index in the specified collection:
collection.createIndex(Indexes.ascending("$**"))
Clustered Index
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)
Atlas Search Index Management
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.
Create Search Index
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.
List Search Indexes
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.
Update Search Indexes
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.
Delete Search Indexes
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.
Text Index
The following example creates a text index on the specified string field:
collection.createIndex(Indexes.text("<field name>"))
Delete an Index
The following example deletes an index with the specified name:
collection.dropIndex("<index name>")