Docs 菜单
Docs 主页
/ / /
Scala

使用索引优化查询

在此页面上

  • Overview
  • 示例应用程序
  • 单字段索引
  • 复合索引
  • Multikey Index
  • 地理空间索引
  • 删除索引
  • Atlas Search 索引管理
  • 创建Atlas Search索引
  • 搜索索引列表
  • 更新搜索索引
  • 删除Atlas Search索引
  • API 文档

在此页面上,您可以查看可复制的代码示例,这些示例展示了如何使用MongoDB Scala驱动程序管理不同类型的索引。

要使用此页面中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。请务必将代码示例中的所有占位符(例如 <connection string URI>)替换为MongoDB 部署的相关值。

您可以使用以下示例应用程序来测试此页面上的代码。要使用示例应用程序,请执行以下步骤:

  1. 确保项目中安装了Scala驾驶员。请参阅 下载和安装指南以学习;了解详情。

  2. 复制以下代码并将其粘贴到新的.scala文件中。

  3. 从此页面复制代码示例,并将其粘贴到文件中的指定行。

import org.mongodb.scala._
import org.mongodb.scala.model.SearchIndexModel
import java.util.concurrent.TimeUnit
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import org.mongodb.scala.model.Indexes
object SearchIndexes {
def main(args: Array[String]): Unit = {
// Create a new client and connect to the server
val mongoClient = MongoClient("<connection string URI>")
val database = mongoClient.getDatabase("<database name>")
val collection = database.getCollection("<collection name>")
// Start example code here
// End example code here
Thread.sleep(1000)
mongoClient.close()
}
}

以下示例在指定字段上创建一个升序索引:

val index = Indexes.ascending("<field name>")
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

要学习;了解有关单字段索引的更多信息,请参阅单字段索引指南。

以下示例在两个指定字段上创建复合索引。

val index = Indexes.compoundIndex(
Indexes.descending("<field name 1>"),
Indexes.ascending("<field name 2>")
)
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

要了解有关复合索引的更多信息,请参阅复合索引指南。

以下示例在指定的数组值字段上创建多键索引:

val index = Indexes.ascending("<field name>")
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

要了解有关多键索引的更多信息,请参阅多键索引指南。

以下示例在包含GeoJSON对象的指定字段上创建2 dsphere索引:

val observable = collection.createIndex(Indexes.geo2dsphere("<2d index>"))
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

有关2 dsphere 索引的更多信息,请参阅MongoDB Server手册中的2 dsphere 索引指南。

有关GeoJSON类型的更多信息,请参阅MongoDB Server手册中的GeoJSON对象指南。

以下示例删除具有指定名称的索引:

val observable = collection.dropIndex("<index name>")
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

以下部分包含描述如何管理Atlas Search索引的代码示例。

要学习;了解有关搜索索引的更多信息,请参阅Atlas Search索引指南。

以下示例在指定字段上创建Atlas Search索引:

val index = Document("mappings" -> Document("dynamic" -> <boolean value>))
val observable = collection.createSearchIndex("<index name>", index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

以下示例将打印指定集合中的Atlas Search索引列表:

val observable = collection.listSearchIndexes()
observable.subscribe(new Observer[Document] {
override def onNext(index: Document): Unit = println(index.toJson())
override def onError(e: Throwable): Unit = println("Error: " + e.getMessage)
override def onComplete(): Unit = println("Completed")
})

以下示例使用指定的新索引定义更新现有Atlas Search索引:

val updateIndex = Document("mappings" -> Document("dynamic" -> false))
val observable = collection.updateSearchIndex("<index to update>", updateIndex)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

以下示例删除具有指定名称的Atlas Search索引:

val observable = collection.dropSearchIndex("<index name>")
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

要学习;了解有关本指南中使用的方法或对象的更多信息,请参阅以下API文档:

后退

对副本集的操作