I want to get the total number of results for an aggregation using mongotemplate.
To clarify - for queries we can use: mongoTemplate.count(query, collectionName)
, which gets the number of results without considering limit / skip.
However, I couldn't find something similar for mongotemplate aggregations... The total results are needed because we use pagination, and need to know the total number without the limit / skip.
EDIT: for example, if I have the following collection A with 10 documents:
{ field1 : ... , field2 : ... , }, ...
I can create a query to get documents using mongotemplate :
Query query = new Query();
query.limit(5);
List<?> queryResults = this.mongoTemplate.find(query, A.class , "A") // will return only 5 results
long count = this.mongoTemplate.count(query, collectionName); // will return 10, because that's the total number result
Now, if I have an aggregation instead of query:
List<AggregationOperation> aggregationOperations = new ArrayList<AggregationOperation>();
aggregationOperations.add(Aggregation.match(...));
aggregationOperations.add(Aggregation.group(...));
aggregationOperations.add(Aggregation.limit(someLimit));
AggregationResults<?> aggregationResults =
this.mongoTemplate.aggregate(
Aggregation.newAggregation(aggregationOperations), "A",
AggregationResults.class);
I want some way to get the total number of results in the aggregation - similar to "count" for the query.