我经常使用MongoTemplate进行聚合操作,但它效率不高。我想"提示"指定自己的索引以提高性能
但是,无法在聚合选项中添加"提示"选项
我看到DBCollection可能能够做到这一点,但我没有找到做到这一点的方法。聚合操作不是DBCollection聚合方法中的参数,聚合操作是我能找到的唯一可以使用"提示"的地方
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.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。
英语不是很好,如果你没有表达清楚,请原谅我。
AggregationOption 中的提示是最近实现的 [SpringDataMongoDB Release 3.1],你可以像
Aggregation
.newAggregation(aggrgationOperations)
.withOptions(AggregationOptions.builder().hint(new Document("fieldName", 1)).build()
或者对于地理空间查询,只需将索引更改为位置字段,如下所示:
AggregationOptions.builder().hint(new Document("fieldName", "2dsphere").build();