MongoTemplate并提供管理索引和集合的方法。 这些方法被收集到一个分别称为 的帮助程序接口中。 可以通过调用该方法并传入集合名称或实体的 (集合名称派生自 ,可以是名称,也可以是从注释元数据派生)来访问这些操作。ReactiveMongoTemplateIndexOperationsReactiveIndexOperationsindexOpsjava.lang.Class.classSpring中文文档

以下列表显示了该接口:IndexOperationsSpring中文文档

public interface IndexOperations {

    String ensureIndex(IndexDefinition indexDefinition);

    void alterIndex(String name, IndexOptions options);

    void dropIndex(String name);

    void dropAllIndexes();

    List<IndexInfo> getIndexInfo();
}
public interface ReactiveIndexOperations {

    Mono<String> ensureIndex(IndexDefinition indexDefinition);

    Mono<Void> alterIndex(String name, IndexOptions options);

    Mono<Void> dropIndex(String name);

    Mono<Void> dropAllIndexes();

    Flux<IndexInfo> getIndexInfo();

创建索引的方法

可以使用 MongoTemplate 类在集合上创建索引以提高查询性能,如以下示例所示:Spring中文文档

template.indexOps(Person.class)
    .ensureIndex(new Index().on("name",Order.ASCENDING));
Mono<String> createIndex = template.indexOps(Person.class)
    .ensureIndex(new Index().on("name",Order.ASCENDING));

ensureIndex确保集合存在所提供的 IndexDefinition 的索引。Spring中文文档

可以使用 和 类创建标准索引、地理空间索引和文本索引。 例如,给定上一节中定义的类,可以声明地理空间查询,如以下示例所示:IndexDefinitionGeoSpatialIndexTextIndexDefinitionVenueSpring中文文档

template.indexOps(Venue.class)
    .ensureIndex(new GeospatialIndex("location"));
Index并支持排序规则的配置。GeospatialIndex
Index并支持排序规则的配置。GeospatialIndex

访问索引信息

该接口具有返回对象列表的方法。 此列表包含集合上定义的所有索引。下面的示例在具有属性的类上定义一个索引:IndexOperationsgetIndexInfoIndexInfoPersonageSpring中文文档

template.indexOps(Person.class)
    .ensureIndex(new Index().on("age", Order.DESCENDING).unique());

List<IndexInfo> indexInfoList = template.indexOps(Person.class)
   .getIndexInfo();
Mono<String> ageIndex = template.indexOps(Person.class)
    .ensureIndex(new Index().on("age", Order.DESCENDING).unique());

Flux<IndexInfo> indexInfo = ageIndex.then(template.indexOps(Person.class)
   .getIndexInfo());

使用集合的方法

下面的示例演示如何创建集合:Spring中文文档

MongoCollection<Document> collection = null;
if (!template.getCollectionNames().contains("MyNewCollection")) {
    collection = mongoTemplate.createCollection("MyNewCollection");
}
MongoCollection<Document> collection = template.getCollectionNames().collectList()
    .flatMap(collectionNames -> {
        if(!collectionNames.contains("MyNewCollection")) {
            return template.createCollection("MyNewCollection");
        }
        return template.getMongoDatabase().map(db -> db.getCollection("MyNewCollection"));
    });
集合创建允许使用排序规则进行自定义并支持排序规则CollectionOptions
与 MongoCollections 交互的方法
集合创建允许使用排序规则进行自定义并支持排序规则CollectionOptions

时间序列

MongoDB 5.0 引入了时间序列集合,这些集合经过优化,可以有效地存储随时间推移的文档,例如测量或事件。 在插入任何数据之前,需要创建这些集合。 可以通过运行命令、定义时间序列集合选项或从注释中提取选项来创建集合,如以下示例所示。createCollection@TimeSeriesSpring中文文档

例 1.创建时序集合
通过MongoDB驱动程序创建时间序列
template.execute(db -> {

    com.mongodb.client.model.CreateCollectionOptions options = new CreateCollectionOptions();
    options.timeSeriesOptions(new TimeSeriesOptions("timestamp"));

    db.createCollection("weather", options);
    return "OK";
});
使用CollectionOptions
template.createCollection("weather", CollectionOptions.timeSeries("timestamp"));
创建派生自 Annotation 的时间序列集合
@TimeSeries(collection="weather", timeField = "timestamp")
public class Measurement {

    String id;
    Instant timestamp;
    // ...
}

template.createCollection(Measurement.class);

上面的代码片段可以很容易地转移到提供相同方法的反应式 API 中。 请确保正确订阅返回的发布者。Spring中文文档