6

I'm using spring mongo template to run an agreegation query on mongodb. I'm wondering is there any way to find out the count of aggregation result in spring mongo template?

Here is my Aggregation sample :

Aggregation agg = newAggregation(Class1.class,
            match(criteria),
            group("prop1", "prop2")
            .avg("x").as("averageX")
        );

I just need to know how to get count of this aggregation result in spring mongo template.

Zhozhe
  • 403
  • 2
  • 5
  • 14
  • Yes there is. But there is also no code here to show what you are doing. – Blakes Seven Sep 25 '15 at 15:02
  • I just have an aggregation using Spring Aggregation Class. i just need to get the result count before getting the result itself. – Zhozhe Sep 25 '15 at 15:03
  • Sounds like you want a "facet" result then, and with paging, like Solr/ElasticSearch. Use two separate queries. It's the correct way to do it. Otherwise just count the number of results. What's the problem with that? Again. Still no code. Clear as mud. Just guessing – Blakes Seven Sep 25 '15 at 15:05
  • 1
    I added the code sample. i can't count the total result because i return only part of result with spring(because of pagination) . so i need a way to count the total result of this aggregation and i don't know how to do so. – Zhozhe Sep 25 '15 at 15:10

3 Answers3

9

My response comes very late but it might help others. To get the count for an aggregation you need to add a new group at the end:

Add at the end of the aggregation -> Aggregation.group().count().as("count") to get the count

Aggregation aggregation = newAggregation(
            Aggregation.match(Criteria.where("x").is(x).and("y").exists(true)),
            Aggregation.group("x", "y"),
            Aggregation.group().count().as("count") 
);

To get the count:

Long.parseLong(results.getMappedResults().get(0).get("count").toString());
Dr.Agos
  • 2,229
  • 2
  • 15
  • 17
1

for spring data mongo 2.1.4

if you assume your return class is like this:

class Output{
  Object x;
  Object y;
  /**getter and setter**/
}

you can use this code:

1.Hold grouping data:

AggregationResults<Output> result = 
                mongoTemplate.aggregate(
                        agg ,
                        "YOUR_DOCUMENT",
                        Output.class
                );
int count = result.getMappedResults().size();

2. only get count: (grouping not effect until you use first(...) or last(...) usage after that)

Aggregation agg = Aggregation.newAggregation(
                      match(criteria),
                      count().as("x")/*using x only for mapping count*/
                   );
    AggregationResults<Output> result = 
                    mongoTemplate.aggregate(
                            agg ,
                            "YOUR_DOCUMENT",
                            Output.class
                    );
    int count = result.getMappedResults().get(0).getX().get(0);/**o_O**/
Amir Azizkhani
  • 1,662
  • 17
  • 30
0

Below worked for me.

List<AggregationOperation> aggOps = new ArrayList<>();
aggOps.add(Aggregation.match(new Criteria("x").is(x)));
aggOps.add(Aggregation.group().count().as("count"));
Aggregation aggRes = Aggregation.newAggregation(aggOps);
AggregationResults<Map> results = mongoTemplate.aggregate(aggRes, "Collection_name", Map.class);
Long count = Long.parseLong(results.getMappedResults().get(0).get("count").toString());

Count variable above have aggregation documents count.

Mahesh Vemula
  • 145
  • 2
  • 14