使用索引优化查询
概述
在本页中,您可以查看可复制的代码示例,展示如何使用Java Reactive Streams驱动程序通过Java Reactive Streams驱动程序管理不同类型的索引。
要使用本页中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。请确保用您MongoDB部署的相关值替换代码示例中的所有占位符,例如<connection string URI>。
项目Reactor实现
本指南使用Project Reactor库来消费由Java Reactive Streams驱动程序方法返回的Publisher实例。要了解有关Project Reactor库以及如何使用它的更多信息,请参阅Reactor文档中的入门。
还有其他方法来消费 Publisher 实例。您可以使用许多替代库之一,例如 RxJava,或者直接调用 Publisher.subscribe() 并传递自己的 Subscriber 实现。
本指南使用 Reactor 的 Mono.block() 方法订阅 Publisher 并阻塞当前线程,直到 Publisher 达到其终止状态。要了解更多关于 Reactive Streams 初始化计划的信息,请参阅 Reactive Streams。
重要
返回的发布者(Publisher)是冷(Cold)的
Java Reactive Streams 驱动方法返回的所有 Publisher 实例都是冷(Cold)的,这意味着除非您订阅返回的 Publisher,否则相应的操作不会发生。我们建议只订阅一次返回的 Publisher,因为多次订阅可能会导致错误。
示例应用
您可以使用以下示例应用来测试本页面的代码示例。要使用示例应用,请按照以下步骤操作
在您的IDE中创建一个新的Java项目。
将Java响应式流驱动程序安装到您的Java项目中。
在您的Java项目中安装Project Reactor库。
复制以下代码并将其粘贴到一个名为
IndexApp.java的新Java文件中。从本页面复制一个代码示例并将其粘贴到文件中指定的行。
1 import com.mongodb.ConnectionString; 2 import com.mongodb.MongoClientSettings; 3 import com.mongodb.ServerApi; 4 import com.mongodb.ServerApiVersion; 5 6 import com.mongodb.client.model.ClusteredIndexOptions; 7 import com.mongodb.client.model.CreateCollectionOptions; 8 import com.mongodb.client.model.IndexOptions; 9 import com.mongodb.client.model.Indexes; 10 import com.mongodb.reactivestreams.client.*; 11 import org.bson.Document; 12 import org.reactivestreams.Publisher; 13 import reactor.core.publisher.Flux; 14 import reactor.core.publisher.Mono; 15 16 public class IndexApp { 17 public static void main(String[] args) { 18 // Replace the placeholder with your Atlas connection string 19 String uri = "<connection string URI>"; 20 21 // Construct a ServerApi instance using the ServerApi.builder() method 22 ServerApi serverApi = ServerApi.builder() 23 .version(ServerApiVersion.V1) 24 .build(); 25 26 MongoClientSettings settings = MongoClientSettings.builder() 27 .applyConnectionString(new ConnectionString(uri)) 28 .serverApi(serverApi) 29 .build(); 30 31 // Create a new client and connect to the server 32 try (MongoClient mongoClient = MongoClients.create(settings)) { 33 MongoDatabase database = mongoClient.getDatabase("<database name>"); 34 MongoCollection<Document> collection = database.getCollection("<collection name>"); 35 36 // Start example code here 37 38 // End example code here 39 } 40 } 41 }
单字段索引
以下示例在指定的字段上创建一个升序索引
Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name>")); Mono.from(publisher).block();
复合索引
以下示例在指定的字段上创建复合索引
Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name 1>", "<field name 2>")); Mono.from(publisher).block();
多键索引
以下示例在指定的数组值字段上创建多键索引
Publisher<String> publisher = collection.createIndex(Indexes.ascending("<array field name>")); Mono.from(publisher).block();
地理空间索引
以下示例在包含GeoJSON对象的指定字段上创建2dsphere索引
Publisher<String> publisher = collection.createIndex(Indexes.geo2dsphere("<GeoJSON object field>")); Mono.from(publisher).block();
唯一索引
以下示例在指定的字段上创建唯一索引
IndexOptions indexOptions = new IndexOptions().unique(true); Publisher<String> publisher = collection.createIndex(Indexes.ascending("<field name>"), indexOptions); Mono.from(publisher).block();
通配符索引
以下示例在指定的集合中创建通配符索引
Publisher<String> publisher = collection.createIndex(Indexes.ascending("$**")); Mono.from(publisher).block();
聚集索引
以下示例创建一个新的集合,在 _id 字段上创建聚集索引
ClusteredIndexOptions clusteredIndexOptions = new ClusteredIndexOptions( Indexes.ascending("_id"), true ); CreateCollectionOptions createCollectionOptions= new CreateCollectionOptions() .clusteredIndexOptions(clusteredIndexOptions); Publisher<Void> clusteredCollection = database.createCollection("<collection name>", createCollectionOptions); Mono.from(clusteredCollection).block();
Atlas 搜索索引管理
以下各节包含描述如何管理 Atlas 搜索索引的代码示例。
创建搜索索引
以下示例在指定的字段上创建 Atlas Search 索引
Document index = new Document("mappings", new Document("dynamic", true)); Publisher<String> publisher = collection.createSearchIndex("<index name>", index); Mono.from(publisher).block();
列出搜索索引
以下示例打印指定集合中 Atlas Search 索引的列表
ListSearchIndexesPublisher<Document> listIndexesPublisher = collection.listSearchIndexes(); Flux.from(listIndexesPublisher) .doOnNext(System.out::println) .blockLast();
更新搜索索引
以下示例使用指定的新索引定义更新现有的 Atlas Search 索引
Document newIndex = new Document("mappings", new Document("dynamic", true)); Publisher<Void> publisher = collection.updateSearchIndex("<index name>", newIndex); Mono.from(publisher).block();
删除搜索索引
以下示例删除具有指定名称的 Atlas Search 索引
Publisher<Void> publisher = collection.dropIndex("<index name>"); Mono.from(publisher).block();
文本索引
以下示例在指定的字符串字段上创建文本索引
Publisher<String> publisher = collection.createIndex(Indexes.text("<field name>")); Mono.from(publisher).block();
删除索引
以下示例删除具有指定名称的索引
Publisher<Void> publisher = collection.dropIndex("<index name>"); Mono.from(publisher).block();