Overview
在此页面上,您可以查看可复制的代码示例,这些示例展示了如何使用Java Reactive Streams驱动程序管理不同类型的索引。
要使用此页面中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如<connection string URI> )替换为 MongoDB 部署的相关值。
项目 Reactor 实施
本指南使用 Project Reactor 库来使用Java Reactive Streams驱动程序方法返回的 Publisher 实例。要学习;了解有关 Project Reactor 库及其使用方法的更多信息,请参阅 Reactor 文档中的入门。
还有其他方法可以使用 Publisher 实例。您可以使用众多替代库(例如 RxJava)之一,也可以直接调用 Publisher.subscribe() 并传递您自己的 Subscriber实施。
本指南使用 Reactor 中的 Mono.block() 方法订阅Publisher 并区块当前线程,直到 Publisher 达到其终止状态。要学习;了解有关 Reactive Streams 计划的更多信息,请参阅 Reactive Streams。
重要
返回的发布者处于冷状态
Java Reactive Streams驱动程序方法返回的所有 Publisher 实例都是冷的,这意味着除非您订阅返回的 Publisher,否则不会发生相应的操作。我们建议仅订阅返回的 Publisher 一次,因为订阅多次可能会导致错误。
示例应用程序
您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:
在 IDE 中创建一个新的Java项目。
在Java项目中安装Java Reactive Streams驱动程序。
在 Java 项目中安装 Project Reactor 库。
复制以下代码并将其粘贴到名为
IndexApp.java的新Java文件中。从此页面复制代码示例,并将其粘贴到文件中的指定行。
import com.mongodb.ConnectionString;import com.mongodb.MongoClientSettings;import com.mongodb.ServerApi;import com.mongodb.ServerApiVersion;
import com.mongodb.client.model.ClusteredIndexOptions;import com.mongodb.client.model.CreateCollectionOptions;import com.mongodb.client.model.IndexOptions;import com.mongodb.client.model.Indexes;import com.mongodb.reactivestreams.client.*;import org.bson.Document;import org.reactivestreams.Publisher;import reactor.core.publisher.Flux;import reactor.core.publisher.Mono;
public class IndexApp { public static void main(String[] args) { // Replace the placeholder with your Atlas connection string String uri = "<connection string URI>";
// Construct a ServerApi instance using the ServerApi.builder() method ServerApi serverApi = ServerApi.builder() .version(ServerApiVersion.V1) .build();
MongoClientSettings settings = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(uri)) .serverApi(serverApi) .build();
// Create a new client and connect to the server try (MongoClient mongoClient = MongoClients.create(settings)) { MongoDatabase database = mongoClient.getDatabase("<database name>"); MongoCollection<Document> collection = database.getCollection("<collection name>");
// Start example code here
// End example code here } }}单字段索引
以下示例在指定字段上创建一个升序索引:
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();Multikey Index
以下示例在指定的数组值字段上创建多键索引:
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();通配符索引(Wildcard Index)
以下示例在指定集合中创建通配符索引:
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();MongoDB 搜索索引管理
以下部分包含描述如何管理MongoDB搜索索引的示例。
创建Atlas Search索引
以下示例在指定字段上创建MongoDB搜索索引:
Document index = new Document("mappings", new Document("dynamic", true));Publisher<String> publisher = collection.createSearchIndex("<index name>", index);Mono.from(publisher).block();搜索索引列表
以下示例打印指定集合中的MongoDB搜索索引列表:
ListSearchIndexesPublisher<Document> listIndexesPublisher = collection.listSearchIndexes();
Flux.from(listIndexesPublisher) .doOnNext(System.out::println) .blockLast();更新搜索索引
以下示例使用指定的新索引定义更新现有MongoDB 搜索索引:
Document newIndex = new Document("mappings", new Document("dynamic", true));Publisher<Void> publisher = collection.updateSearchIndex("<index name>", newIndex);Mono.from(publisher).block();删除Atlas Search索引
以下示例删除具有指定名称的MongoDB 搜索索引:
Publisher<Void> publisher = collection.dropIndex("<index name>");Mono.from(publisher).block();Text Index
以下示例在指定string字段上创建文本索引:
Publisher<String> publisher = collection.createIndex(Indexes.text("<field name>"));Mono.from(publisher).block();删除索引
以下示例删除具有指定名称的索引:
Publisher<Void> publisher = collection.dropIndex("<index name>");Mono.from(publisher).block();