zmxncbv
zmxncbv

Reputation: 51

Cursor with Spring Data Mongo Aggregation

Is there a way to return a cursor with spring data's mongodb aggregation?

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());

Upvotes: 2

Views: 5435

Answers (2)

Gaurav Singh
Gaurav Singh

Reputation: 307

Solution Quoting here :

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);

Upvotes: 5

zmxncbv
zmxncbv

Reputation: 51

Spring data for mongodb does not support the use of a cursor when aggregating. The MongoDB java driver must be used instead.

Upvotes: 3

Related Questions