Java 为什么Spring Data MongoDB不提供;提示“;用于聚合操作的选项,mongodb驱动程序有,但我没有';我不知道如何使用它
我经常使用MongoTemplate进行聚合操作,但效率不高。我想“提示”指定我自己的索引以提高性能 但是,不能在AggregationOptions中添加“提示”选项 我看到DBCollection可能能够做到这一点,但我没有找到一种方法来做到这一点。AggregateOperation不是DBCollection的聚合方法中的参数,AggregateOperation是我能找到的唯一可以使用“提示”的地方 1.org.springframework.data.mongodb.core.aggregation.AggregationOption可用参数:Java 为什么Spring Data MongoDB不提供;提示“;用于聚合操作的选项,mongodb驱动程序有,但我没有';我不知道如何使用它,java,mongodb,spring-data-mongodb,Java,Mongodb,Spring Data Mongodb,我经常使用MongoTemplate进行聚合操作,但效率不高。我想“提示”指定我自己的索引以提高性能 但是,不能在AggregationOptions中添加“提示”选项 我看到DBCollection可能能够做到这一点,但我没有找到一种方法来做到这一点。AggregateOperation不是DBCollection的聚合方法中的参数,AggregateOperation是我能找到的唯一可以使用“提示”的地方 1.org.springframework.data.mongodb.core.agg
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);
}
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;
}
2.com.mongodb.AggregationOptions可用参数:
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);
}
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
英语不是很好,如果你表达不清楚,请原谅。聚合选项中的提示是最近实施的[SpringDataMongoDB 3.1版],你可以像这样使用它
Aggregation
.newAggregation(aggrgationOperations)
.withOptions(AggregationOptions.builder().hint(new Document("fieldName", 1)).build()
或者,对于地理空间查询,只需将索引更改为位置字段,如下所示:
AggregationOptions.builder().hint(new Document("fieldName", "2dsphere").build();
2020年同样的问题