Docs Menu
Docs Home
/ / /
Java Sync
/ /

Indexes Builders

On this page

  • Overview
  • Ascending Indexes
  • Descending Indexes
  • Compound Indexes
  • Text Indexes
  • Hashed Indexes
  • Geospatial Indexes

In this guide, you can learn how to specify indexes using builders in the MongoDB Java driver. The Indexes builder provides helper methods for constructing the following types of indexes:

Indexes store a subset of the collection’s data set. The index stores the value of a specific field or set of fields, ordered by the value of the field. See our guide on Indexes for examples of queries covered by indexes.

The Indexes class provides static factory methods for all the MongoDB index types. Each method returns a BSON instance, which you can pass to createIndex().

Tip

For brevity, you may choose to import all methods of the Indexes class statically:

import static com.mongodb.client.model.Indexes.*;

The following examples assume this static import.

An ascending index enables you to sort query results by the value of the indexed fields from smallest to largest.

In order to create an ascending index, first call the ascending() builder method to create a Bson instance that represents the index document, passing the name or names of the fields you want to index. Then, call the createIndex() method on the collection, passing the Bson instance that contains the index document.

Note

If you have an ascending or a descending index on a single field, MongoDB can sort using the index in either direction.

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

Bson ascendingIndex = ascending("name");
collection.createIndex(ascendingIndex);

A descending index enables you to sort query results by the value of the indexed fields from largest to smallest.

In order to create a descending index, first call the descending() builder method to create a Bson instance that represents the index document, passing the name or names of the fields you want to index. Then, call the createIndex() method on the collection, passing the Bson instance that contains the index document.

The following example specifies a descending index on the capacity field:

Bson descendingIndex = descending("capacity");
collection.createIndex(descendingIndex);

In order to create a compound index, first call the compoundIndex() builder method to create a Bson instance that represents the index document, passing the names of the fields you want to index. Then, call the createIndex() method on the collection, passing the Bson instance that contains the index document.

The following example specifies a compound index composed of descending index on the capacity and year field, followed by an ascending index on the name field:

Bson compoundIndexExample = compoundIndex(descending("capacity", "year"), ascending("name"));
collection.createIndex(compoundIndexExample);

A text index groups documents by the text in the indexed field.

In order to create a text index, first call the text() builder method to create a Bson instance that represents the index document, passing the name of the fields you want to index. Then, call the createIndex() method on the collection, passing the Bson instance that contains the index document.

The following example specifies a text index key on the "theaters" field:

Bson textIndex = text("theaters");
collection.createIndex(textIndex);

A hashed index groups documents by the hash value in the indexed field.

In order to create a hashed index, first call the hashed() builder method to create a Bson instance that represents the index document, passing the name of the fields you want to index. Then, call the createIndex() method on the collection, passing the Bson instance that contains the index document.

The following example specifies a hashed index on the capacity field:

Bson hashedIndex = hashed("capacity");
collection.createIndex(hashedIndex);

A 2dsphere index groups documents by the coordinates in the indexed field.

In order to create a 2dsphere index, first call the geo2dsphere() builder method to create a Bson instance that represents the index document, passing the name or names of the fields you want to index. Then, call the createIndex() method on the collection, passing the Bson instance that contains the index document.

The following example specifies a 2dsphere index on the location field:

Bson geo2dsphereIndex = geo2dsphere("location");
collection.createIndex(geo2dsphereIndex);

Back

Filters Builders