Docs Menu
Docs Home
/ / /
Java Reactive Streams Driver
/

Create Indexes

On this page

  • Prerequisites
  • Connect to a MongoDB Deployment
  • Ascending Index
  • Descending Index
  • Compound Indexes
  • Text Indexes
  • Hashed Index
  • Geospatial Indexes
  • IndexOptions
  • 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 Java Reactive Streams 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 com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoDatabase;
import com.mongodb.reactivestreams.client.MongoCollection;
import org.bson.Document;
import com.mongodb.client.model.Indexes;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.Filters;

Important

This guide uses the Subscriber implementations, which are described 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:

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = 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"))
.subscribe(new PrintToStringSubscriber<String>());

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

collection.createIndex(Indexes.ascending("stars", "name"))
.subscribe(new PrintToStringSubscriber<String>());

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"))
.subscribe(new PrintToStringSubscriber<String>());

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

collection.createIndex(Indexes.descending("stars", "name"))
.subscribe(new PrintToStringSubscriber<String>());

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"))
).subscribe(new PrintToStringSubscriber<String>());

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() static helper method.

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

collection.createIndex(Indexes.text("name"))
.subscribe(new PrintToStringSubscriber<String>());

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"))
.subscribe(new PrintToStringSubscriber<String>());

To support geospatial queries, MongoDB supports various geospatial indexes.

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"))
.subscribe(new PrintToStringSubscriber<String>());

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 com.mongodb.client.model.IndexOptions;

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

IndexOptions indexOptions = new IndexOptions().unique(true);
collection.createIndex(Indexes.ascending("name", "stars"), indexOptions)
.subscribe(new PrintToStringSubscriber<String>());

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".

IndexOptions partialFilterIndexOptions = new IndexOptions()
.partialFilterExpression(Filters.exists("contact.email"));
collection.createIndex(
Indexes.descending("name", "stars"), partialFilterIndexOptions)
.subscribe(new PrintToStringSubscriber<String>());

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

collection.listIndexes().subscribe(new PrintDocumentSubscriber());

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

Back

Databases and Collections