Docs Menu
Docs Home
/ / /
Scala

Optimize Queries by Using Indexes

On this page

  • Overview
  • Sample Application
  • Single Field Index
  • Compound Index
  • Multikey Index
  • Geospatial Index
  • Drop Index
  • Atlas Search Index Management
  • Create Search Index
  • List Search Indexes
  • Update Search Indexes
  • Delete Search Indexes
  • API Documentation

On this page, you can see copyable code examples that show how to manage different types of indexes by using the MongoDB Scala Driver.

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 on this page. To use the sample application, perform the following steps:

  1. Ensure you have the Scala driver installed in your project. See the Download and Install guide to learn more.

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

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

import org.mongodb.scala._
import org.mongodb.scala.model.SearchIndexModel
import java.util.concurrent.TimeUnit
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import org.mongodb.scala.model.Indexes
object SearchIndexes {
def main(args: Array[String]): Unit = {
// Create a new client and connect to the server
val mongoClient = MongoClient("<connection string URI>")
val database = mongoClient.getDatabase("<database name>")
val collection = database.getCollection("<collection name>")
// Start example code here
// End example code here
Thread.sleep(1000)
mongoClient.close()
}
}

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

val index = Indexes.ascending("<field name>")
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

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

The following example creates a compound index on the two specified fields.

val index = Indexes.compoundIndex(
Indexes.descending("<field name 1>"),
Indexes.ascending("<field name 2>")
)
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

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

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

val index = Indexes.ascending("<field name>")
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

To learn more about multikey indexes, see the Multikey Indexes guide.

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

val observable = collection.createIndex(Indexes.geo2dsphere("<2d index>"))
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

For more information on 2dsphere indexes, see the 2dsphere Indexes guide in the MongoDB Server manual.

For more information about the GeoJSON type, see the GeoJSON Objects guide in the MongoDB Server manual.

The following example deletes an index with the specified name:

val observable = collection.dropIndex("<index name>")
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

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

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

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

val index = Document("mappings" -> Document("dynamic" -> <boolean value>))
val observable = collection.createSearchIndex("<index name>", index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

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

val observable = collection.listSearchIndexes()
observable.subscribe(new Observer[Document] {
override def onNext(index: Document): Unit = println(index.toJson())
override def onError(e: Throwable): Unit = println("Error: " + e.getMessage)
override def onComplete(): Unit = println("Completed")
})

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

val updateIndex = Document("mappings" -> Document("dynamic" -> false))
val observable = collection.updateSearchIndex("<index to update>", updateIndex)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

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

val observable = collection.dropSearchIndex("<index name>")
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

To learn more about the methods or objects used in this guide, see the following API documentation:

Back

Operations on Replica Sets