我经常使用MongoTemplate进行聚合操作,但是效率不高。我想“提示”指定我自己的索引以提高性能
但是,不能在AggregationOptions中添加“提示”选项
我看到DBCollection可以做到这一点,但是我没有找到一种方法来做到这一点。 AggregateOperation不是DBCollection聚合方法中的参数,而AggregateOperation是我唯一可以使用“提示”的地方
mongodb version is4.0.4
Spring-data-mongodb version is 2.1.4
The mongodb-driver version is 3.8.2
JDK 11
1.org.springframework.data.mongodb.core.aggregation.AggregationOption可用参数:
public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Document cursor,
@Nullable Collation collation) {
this.allowDiskUse = allowDiskUse;
this.explain = explain;
this.cursor = Optional.ofNullable(cursor);
this.collation = Optional.ofNullable(collation);
}
2.com.mongodb.AggregationOptions可用参数:
AggregationOptions(AggregationOptions.Builder builder) {
this.batchSize = builder.batchSize;
this.allowDiskUse = builder.allowDiskUse;
this.outputMode = builder.outputMode;
this.maxTimeMS = builder.maxTimeMS;
this.bypassDocumentValidation = builder.bypassDocumentValidation;
this.collation = builder.collation;
}
我只想通过索引提高查询效率。查询速度太慢。复杂的查询需要20秒。简单查询也需要4〜5s。
英语不是很好,如果您表达不清楚,请原谅我。
答案 0 :(得分:0)
AggregationOption中的提示最近在[SpringDataMongoDB Release 3.1]中实现,您可以像这样使用
Aggregation
.newAggregation(aggrgationOperations)
.withOptions(AggregationOptions.builder().hint(new Document("fieldName", 1)).build()
或对于geoSpatial查询,只需将索引更改为位置字段,例如:
AggregationOptions.builder().hint(new Document("fieldName", "2dsphere").build();