[英]Spring Data MongoDB Aggregation $out pipeline
我正在使用 Spring 數據 mongoDB 並使用聚合來獲取文檔。
List<AggregationOperation> operationsList = new ArrayList<AggregationOperation>();
operationsList.add(Aggregation.unwind("calendarEvent"));
operationsList.add(Aggregation.match(criteria));
operationsList.add(getMacroEventProjectionFields());
if (start <= 0) {
start = 1;
}
operationsList.add(Aggregation.skip(start - 1));
if (limit > 0) {
operationsList.add(Aggregation.limit(limit));
}
Aggregation aggregation = Aggregation.newAggregation(operationsList);
AggregationResults<MacroEvent> groupResults = mongoTemplate.aggregate(
aggregation, mongoTemplate.getCollectionName(KALiEvent.class),
MacroEvent.class);
該集合包含數千條記錄,直到昨天它都運行良好。 但是今天它已經開始拋出以下異常:
Error [exception: aggregation result exceeds maximum document size (16MB)]
我用谷歌搜索了這個問題,發現聚合只能返回等於 MongoDB 文檔大小的結果,即16MB 。 我為此找到的唯一解決方法是使用$out管道,它執行以下操作:
獲取聚合管道返回的文檔並將它們寫入指定的集合。 $out 運算符必須是管道中的最后一個階段。 $out 運算符讓聚合框架返回任何大小的結果集。
但是,我無法弄清楚如何使用 Spring 數據 MongoDB 來完成此操作,以及如何在上面粘貼的代碼中使用此$out 。
為了避免 16 mb 的異常,我創建了自己的 OutOperation 類來執行 mongo $out管道操作:
package com.myop;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
public class OutOperation implements AggregationOperation {
/**
the name of the collection where aggregation data will be stored
*/
private String collectionName;
public OutOperation(String collectionName) {
this.collectionName = collectionName;
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return new BasicDBObject("$out", collectionName);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.