[英]Spring Data MongoDB - how to set maxTimeMS for Aggregation pipeline query
我注意到 org.springframework.data.mongodb.core.aggregation.AggregationOption class 僅涵蓋 MongoDB 聚合管道文檔中描述的這些選項的一小部分: https://docs.mongodb.com/mandual/mandual聚合/#syntax
我需要設置 maxTimeMS 選項,但它在 org.springframework.data.mongodb.core.aggregation.AggregationOption 中不可用:
public class AggregationOptions {
private static final String BATCH_SIZE = "batchSize";
private static final String CURSOR = "cursor";
private static final String EXPLAIN = "explain";
private static final String ALLOW_DISK_USE = "allowDiskUse";
private static final String COLLATION = "collation";
private static final String COMMENT = "comment";
...
然而,另一個 class(mongodb-driver 的)實際上有這樣的字段 maxTimeMS,com.mongodb.AggregationOptions:
public class AggregationOptions {
private final Integer batchSize;
private final Boolean allowDiskUse;
private final OutputMode outputMode;
private final long maxTimeMS;
private final Boolean bypassDocumentValidation;
private final Collation collation;
...
任何想法/技巧如何使用 Spring 數據 MongoDB API 為聚合查詢設置此 maxTimeMS? 或者我是否需要使用本機查詢來構建/編寫此類聚合?
順便提一句。 我知道 Spring 數據 MongoDB 支持 maxTimeMS 進行查找操作,例如:
Query.query(mongoCriteria).with(pageable).maxTime(Duration.ofMinutes(4))
但是我需要在服務器端設置聚合查詢處理超時,以防止“永無止境”的查詢會降低性能。
彈簧數據 mongodb:2.2.0.RELEASE
我試圖為我的查詢設置一個時間限制,我花了一些時間才找到答案,所以我想與任何可能感興趣的人分享我的方法:
AggregationOptions aggregationOptions = new AggregationOptions.Builder().maxTime(Duration.ofSeconds(3)).build();
Aggregation aggregate = Aggregation.newAggregation(match).withOptions(aggregationOptions);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.