带有Spring Data Mongo Aggregation的游标

时间:2016-10-21 15:19:01

标签: mongodb aggregation-framework

有没有办法使用spring数据的mongodb聚合返回游标?

Aggregation agg = newAggregation(
            match(Criteria.where("_id").is(objId)),
            unwind("taskResultContent"),
            project("taskResultContent.executionUUID","taskResultContent.returnContent","taskResultContent.sequency").and("resultID").previousOperation(),
            match(Criteria.where("executionUUID").is(executionUUID)),
            sort(DESC,"sequency")
        ).withOptions(Aggregation.newOptions().cursor(cursor).build());

2 个答案:

答案 0 :(得分:2)

mongodb的Spring数据在聚合时不支持使用游标。必须使用MongoDB java驱动程序。

答案 1 :(得分:2)

Solution 引用这里:

从spring-data-mongo版本2.0.0.M4开始(AFAIK)MongoTemplate得到了一个aggregateStream方法。

所以你可以做到以下几点:

 AggregationOptions aggregationOptions = Aggregation.newAggregationOptions()
    // this is very important: if you do not set the batch size, 
    // you'll get all the objects at once and you might run out of memory
    // if the returning data set is too large
    .cursorBatchSize(mongoCursorBatchSize)
    .build();

data = mongoTemplate.aggregateStream(Aggregation.newAggregation(
       Aggregation.group("person_id")
                  .count()
                  .as("count"))
                  .withOptions(aggregationOptions), collectionName, YourClazz.class);