Spring Data Mongo 聚合的游标

zmx*_*cbv 2 mongodb aggregation-framework

有没有办法用 spring data 的 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());
Run Code Online (Sandbox Code Playgroud)

Gau*_*ngh 5

解决方案 在这里引用:

从 spring-data-mongo 版本 2.0.0.M4 开始(据我所知)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);
Run Code Online (Sandbox Code Playgroud)