Docs Menu
Docs Home
/ / /
Kotlin Coroutine
/ /

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 Kotlin Driver. The Indexes builder provides helper methods for constructing the following types of indexes:

  • Ascending Indexes

  • Descending Indexes

  • Compound Indexes

  • Text Indexes

  • Hashed Indexes

  • Geospatial 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:

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

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:

val ascendingIndex = Indexes.ascending("name")
val indexName = collection.createIndex(ascendingIndex)
println(indexName)
name_1

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:

val descendingIndex = Indexes.descending("capacity")
val indexName = collection.createIndex(descendingIndex)
println(indexName)
capacity_-1

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:

val compoundIndexExample = Indexes.compoundIndex(
Indexes.descending("capacity", "year"),
Indexes.ascending("name")
)
val indexName = collection.createIndex(compoundIndexExample)
println(indexName)
capacity_-1_year_-1_name_1

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:

val textIndex = Indexes.text("theaters")
val indexName = collection.createIndex(textIndex)
println(indexName)
theaters_text

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:

val hashedIndex = Indexes.hashed("capacity")
val indexName = collection.createIndex(hashedIndex)
println(indexName)
capacity_hashed

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:

val geo2dsphereIndex = Indexes.geo2dsphere("location")
val indexName = collection.createIndex(geo2dsphereIndex)
println(indexName)
location_2dsphere

Back

Filters Builders