AggregateOutput目前已经被废弃
。
AggregationOutput output = collection.aggregate(pipe);
for (DBObject obj : output.results()) {
//...
}
之前的替换方案AggregationOptions目前也被废弃,.outputMode(AggregationOptions.OutputMode.INLINE) 也已弃用
。
// this build of options is taken from the original method
// aggregate(pipe) to have same behaviour
AggregationOptions options = AggregationOptions.builder()
.outputMode(AggregationOptions.OutputMode.INLINE)
.build();
Cursor cursor = collection.aggregate(pipe,options);
while (cursor.hasNext() ) {
DBObject obj = cursor.next();
//...
}
那么,目前可供选择的方案是什么呢?
比如我们现在有这么个例子
。
List<DBObject> studentAggregationQuery = new ArrayList<>();
studentAggregationQuery.add(new BasicDBObject("$roll", roll));
studentAggregationQuery.add(new BasicDBObject("$marks", marks));
AggregationOutput studentOutput = collection.aggregate(studentAggregationQuery);
List<StudentData> studentDataList = new ArrayList<>();
studentOutput.results().forEach(dbObject -> {
orderMetricsList.add(new
StudentData(mongoTemplate.getConverter().read(Students.class,
(Integer) dbObject.get("roll"), (Double) dbObject.get("marks")));
});
我们选择用AggregateIterable 替换 AggregationOutput 以获得所需的输出/结果
。
**最终的替换代码如图所示如下**
。
List<Document> studentAggregationQuery = new ArrayList<>();
studentAggregationQuery.add(new Document("$roll", roll));
studentAggregationQuery.add(new Document("$marks", marks));
AggregateIterable studentDataIterator =
collection.aggregate(studentAggregationQuery);
List<StudentData> studentDataList = new ArrayList<>();
MongoCursor<Document> studentIterator = studentDataIterator.iterator();
while (studentIterator.hasNext()) {
Document next = studentIterator.next();
studentDataList.add(new
StudentData(mongoTemplate.getConverter().read(Students.class,
(Integer) next.get("roll"), (Double) next.get("marks")));
}