我经常使用 MongoTemplate 进行聚合操作,但效率不高。我想“提示”指定我自己的索引来提高性能但是AggregationOptions中不能添加“hint”选项我看到 DBCollection 可能能够执行此操作,但我没有找到执行此操作的方法。AggregateOperation 不是 DBCollection 聚合方法中的参数,AggregateOperation 是我能找到的唯一可以使用“提示”的地方mongodb version is4.0.4Spring-data-mongodb version is 2.1.4The mongodb-driver version is 3.8.2JDK 111.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。英文不是很好,表达不清楚请见谅。
1 回答
波斯汪
TA贡献1811条经验 获得超4个赞
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();
添加回答
举报
0/150
提交
取消