此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data MongoDB 4.4.0! |
索引和集合管理
MongoTemplate
和ReactiveMongoTemplate
提供管理索引和集合的方法。
这些方法被收集到一个名为IndexOperations
分别ReactiveIndexOperations
.
您可以通过调用indexOps
方法并传入集合名称或java.lang.Class
的 Git(集合名称派生自.class
,按名称或来自注释元数据)。
下面的清单显示了IndexOperations
接口:
-
Imperative
-
Reactive
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 类在集合上创建索引以提高查询性能,如下例所示:
-
Imperative
-
Reactive
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 的索引。
您可以使用IndexDefinition
,GeoSpatialIndex
和TextIndexDefinition
类。
例如,给定Venue
类,您可以声明地理空间查询,如下例所示:
template.indexOps(Venue.class)
.ensureIndex(new GeospatialIndex("location"));
Index 和GeospatialIndex 支持排序规则的配置。 |
访问索引信息
这IndexOperations
interface 具有getIndexInfo
方法返回IndexInfo
对象。
此列表包含在集合上定义的所有索引。以下示例在Person
类具有age
财产:
-
Imperative
-
Reactive
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());
使用集合的方法
以下示例显示如何创建集合:
-
Imperative
-
Reactive
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 交互的方法
-
getCollectionNames:返回一组集合名称。
-
collectionExists:检查是否存在具有给定名称的集合。
-
createCollection:创建无上限的集合。
-
dropCollection:删除集合。
-
getCollection:按名称获取集合,如果不存在,则创建该集合。
时间序列
MongoDB 5.0 引入了时间序列集合,这些集合经过优化,可以随着时间的推移有效地存储文档,例如测量或事件。
在插入任何数据之前,需要按原样创建这些集合。
可以通过运行createCollection
命令、定义时间序列集合选项或从@TimeSeries
annotation 中,如以下示例所示。
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"));
@TimeSeries(collection="weather", timeField = "timestamp")
public class Measurement {
String id;
Instant timestamp;
// ...
}
template.createCollection(Measurement.class);
上面的代码片段可以很容易地转移到提供完全相同方法的反应式 API 中。 确保正确订阅返回的发布者。
您可以使用 |