Streaming the result of an aggregate operation using spring-data-mongodb

前端 未结 1 1941
[愿得一人]
[愿得一人] 2021-01-14 05:25

I am using spring-data-mongodb and I want to use a cursor for an aggregate operation.

MongoTemplate.stream() gets a Query, so I tried creating the Aggregati

相关标签:
1条回答
  • 2021-01-14 06:10

    For those who are still trying to find the answer to this:

    From spring-data-mongo version 2.0.0.M4 onwards (AFAIK) MongoTemplate got an aggregateStream method.

    So you can do the following:

     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);
    
    0 讨论(0)
提交回复
热议问题