For the most recent version of the reference documentation, see our MongoDB Java Driver documentation site.
- Builders
- Indexes
Indexes
The Indexes
class provides static factory methods for all the MongoDB Index key types.
Each method returns an instance of the Bson
type, which can in turn be used with the createIndex
methods.
For brevity, you may choose to import the methods of the Indexes
class statically:
import static com.mongodb.client.model.Indexes.*;
All the examples below assume this static import.
Ascending
To specify an ascending index key, use one of the ascending
methods.
This example specifies an ascending index key for the quantity
field:
ascending("quantity")
This example specifies a compound index key composed of the quantity
field sorted in ascending order and the totalAmount
field
sorted in ascending order:
ascending("quantity", "totalAmount")
Descending
To specify a descending index key, use one of the descending
methods.
This example specifies a descending index key on the quantity
field:
descending("quantity")
This example specifies a compound index key composed of the quantity
field sorted in descending order and the totalAmount
field
sorted in descending order:
descending("quantity", "totalAmount")
Compound indexes
To specify a compound index, use the compoundIndex
method.
This example specifies a compound index key composed of the quantity
field sorted in ascending order, followed by the totalAmount
field
sorted in ascending order, followed by the orderDate
field sorted in descending order:
compoundIndex(ascending("quantity", "totalAmount"), descending("orderDate"))
Text Index
To specify a text index key, use the text
method.
This example specifies a text index key for the description
field:
text("description")
This example specifies a text index key for every field that contains string data:
text()
Hashed Index
To specify a hashed index key, use the hashed
method.
This example specifies a hashed index key for the timestamp
field:
hashed("timestamp")
Geospatial Indexes
There are also helpers for creating the index keys for the various geospatial indexes supported by mongodb.
2dsphere
To specify a 2dsphere index key, use one of the geo2dsphere
methods.
This example specifies a 2dsphere index on the location
field:
geo2dsphere("location")
2d
To specify a 2d index key, use the geo2d
method.
important
A 2d index is for data stored as points on a two-dimensional plane and is intended for legacy coordinate pairs used in MongoDB 2.2 and earlier.
This example specifies a 2d index on the points
field:
geo2d("points")
geoHaystack
To specify a geoHaystack index key, use the geoHaystack
method.
important
For queries that use spherical geometry, a 2dsphere index is a better option than a haystack index. 2dsphere indexes allow field reordering; geoHaystack indexes require the first field to be the location field. Also, geoHaystack indexes are only usable via commands and so always return all results at once.
This example specifies a geoHaystack index on the position
field and an ascending index on the type
field:
geoHaystack("position", ascending("type"))