对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

索引构建器

在本指南中,您可以了解如何使用 MongoDB Kotlin 驱动程序中的 构建器 指定 索引 Indexes构建器提供了用于构建以下类型索引的辅助方法:

索引存储集合数据集的子集。 索引存储特定字段或字段集的值,按字段值排序。 有关索引涵盖的查询示例,请参阅我们的索引指南。

Indexes 类为所有MongoDB索引类型提供静态工厂方法。每个方法都返回一个BSON实例,您可以将其传递给createIndex().

提示

为简洁起见,您可以选择导入 Indexes 类的所有方法:

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

提示

构建器方法和数据类属性

通过将可选的Kotlin驱动程序扩展依赖项添加到应用程序,您可以直接将构建者类中的方法与数据类属性一起使用。要学习;了解更多信息和查看示例,请参阅《使用构建器与数据类》指南。

升序索引使您能够按索引字段的值从小到大对查询结果进行排序。

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.

注意

如果单个字段有升序或降序索引,MongoDB 可以使用任一方向的索引进行排序。

以下示例对name字段指定升序索引:

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

降序索引使您能够按索引字段的值从大到小对查询结果进行排序。

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.

以下示例指定对 capacity 字段进行降序索引:

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.

以下示例指定了一个复合索引,由capacityyear字段的降序索引以及name字段的升序索引组成:

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

文本索引按索引字段中的文本对文档进行分组。

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.

以下示例在theaters字段上指定文本索引键:

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

哈希索引按索引字段中的哈希值对文档进行分组。

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.

以下示例在capacity字段上指定哈希索引:

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

2dsphere索引按索引字段中的坐标对文档进行分组。

要创建2dsphere 索引,请先调用 geo2 dsphere() Bson构建器方法创建表示索引文档的 实例,并传递要索引的字段的名称。然后,对该集合调用createIndex() Bson方法,并传递包含索引文档的 实例。

以下示例在location字段上指定2dsphere索引:

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