使用索引优化查询
在此页面上
Overview
在此页面上,您可以查看可复制的代码示例,这些示例展示了如何使用MongoDB Scala驱动程序管理不同类型的索引。
要使用此页面中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。请务必将代码示例中的所有占位符(例如 <connection string URI>
)替换为MongoDB 部署的相关值。
示例应用程序
您可以使用以下示例应用程序来测试此页面上的代码。要使用示例应用程序,请执行以下步骤:
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))
要了解有关复合索引的更多信息,请参阅复合索引指南。
Multikey Index
以下示例在指定的数组值字段上创建多键索引:
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索引指南。
创建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索引
以下示例删除具有指定名称的Atlas Search索引:
val observable = collection.dropSearchIndex("<index name>") Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))
API 文档
要学习;了解有关本指南中使用的方法或对象的更多信息,请参阅以下API文档: