Docs Menu
Docs Home
/ / /
Scala
/

Create Indexes

On this page

  • Prerequisites
  • Connect to a MongoDB Deployment
  • Ascending Index
  • Single Ascending Index
  • Compound Ascending Index
  • Descending Index
  • Single Descending Key Index
  • Compound Descending Key Index
  • Compound Indexes
  • Text Indexes
  • Hashed Index
  • Geospatial Indexes
  • 2dsphere
  • IndexOptions
  • Unique Index
  • Partial Index
  • Get a List of Indexes on a Collection

Indexes support the efficient execution of queries in MongoDB. To create an index on a field or fields, pass an index specification document to the MongoCollection.createIndex() method.

The Scala driver provides the Indexes class that includes static factory methods to create index specification documents for the various MongoDB index key types. To learn more about index types, see Indexes in the Server manual.

Note

MongoDB only creates an index if an index of the same specification does not already exist.

You must include the following import statements in your program to run the code examples in this guide:

import org.mongodb.scala._
import org.mongodb.Indexes
import org.mongodb.IndexOptions
import org.mongodb.Filters

Note

This guide uses the Observable implicits as covered in the Quick Start Primer.

First, connect to a MongoDB deployment, then declare and define MongoDatabase and MongoCollection instances.

The following code connects to a standalone MongoDB deployment running on localhost on port 27017. Then, it defines the database variable to refer to the test database and the collection variable to refer to the restaurants collection:

val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("test")
val collection: MongoCollection[Document] = database.getCollection("restaurants")

To learn more about connecting to MongoDB deployments, see the Connect to MongoDB tutorial.

To create a specification for an ascending index, use the Indexes.ascending() static helper method.

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

collection.createIndex(Indexes.ascending("name"))
.printResults()

The following example creates an ascending compound index on the stars field and the name field:

collection.createIndex(Indexes.ascending("stars", "name"))
.printResults()

To view an alternative way to create a compound index, see the Compound Indexes section.

To create a specification of a descending index, use the Indexes.descending() static helper method.

The following example creates a descending index on the stars field:

collection.createIndex(Indexes.descending("stars"))
.printResults()

The following example creates a descending compound index on the stars field and the name field:

collection.createIndex(Indexes.descending("stars", "name"))
.printResults()

To view an alternative way to create a compound index, see the Compound Indexes section.

To create a specification for a compound index, use the Indexes.compoundIndex() static helper method.

Note

To create a specification for a compound index where all the keys are ascending, you can use the ascending() method. To create a specification for a compound index where all the keys are descending, you can use the descending() method.

The following example creates a compound index on the stars field in descending order and the name field in ascending order:

collection.createIndex(
Indexes.compoundIndex(Indexes.descending("stars"),
Indexes.ascending("name")))
.printResults()

MongoDB provides text indexes to support text search of string content. Text indexes can include any field whose value is a string or an array of string elements. To create a specification for a text index, use the Indexes.text() helper method.

The following example creates a text index on the name field:

collection.createIndex(Indexes.text("name"))
.printResults()

To learn more about text indexes, see Text Indexes in the Server manual.

To create a specification for a hashed index index, use the Indexes.hashed() static helper method.

The following example creates a hashed index on the _id field:

collection.createIndex(Indexes.hashed("_id"))
.printResults()

To learn more about hashed indexes, see Hashed Indexes in the Server manual.

To support geospatial queries, MongoDB supports various geospatial indexes. To learn more about geospatial indexes, see Geospatial Indexes in the Server manual.

To create a specification for a 2dsphere index, use the Indexes.geo2dsphere() static helper method.

The following example creates a 2dsphere index on the contact.location field:

collection.createIndex(Indexes.geo2dsphere("contact.location"))
.printResults()

In addition to the index specification document, the createIndex() method can take an index options document that directs the driver to create unique indexes or partial indexes.

The driver provides the IndexOptions class to specify various index options.

Add the following import statement to your code to create an IndexOptions instance.

import org.mongodb.scala.model.IndexOptions

The following code specifies the unique(true) option to create a unique index on the name and stars fields:

val indexOptions = IndexOptions().unique(true)
collection.createIndex(Indexes.ascending("name", "stars"), indexOptions)
.printResults()

To learn more about unique indexes, see Unique Indexes in the Server manual.

To create a partial index, include the partialFilterExpression index option.

The following example creates a partial index on documents in which the value of the status field is "A".

val partialFilterIndexOptions = IndexOptions()
.partialFilterExpression(Filters.exists("contact.email"))
collection.createIndex(
Indexes.descending("name", "stars"), partialFilterIndexOptions)
.printResults()

To learn more about partial indexes, see Partial Indexes in the Server manual.

Use the listIndexes() method to get a list of indexes. The following code lists the indexes on the collection:

collection.listIndexes().printResults()

To learn about other index options, see Index Properties in the Server manual.

Back

Databases and Collections