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 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.
Sample Application
You can use the following sample application to test the code on this page. To use the sample application, perform the following steps:
Ensure you have the Scala driver installed in your project. See the Download and Install guide to learn more.
Copy the following code and paste it into a new
.scala
file.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() } }
Single Field Index
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.
Compound Index
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.
Multikey Index
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.
Geospatial Index
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.
Drop Index
The following example deletes an index with the specified name:
val observable = collection.dropIndex("<index name>") Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))
Atlas Search Index Management
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.
Create Search Index
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))
List Search Indexes
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") })
Update Search Indexes
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))
Delete Search Indexes
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))
API Documentation
To learn more about the methods or objects used in this guide, see the following API documentation: