5

My following mongodb query works as expected

db.importedDataItems.aggregate({
    $match: {
        mobile: "1234567890"
    }
}, {
    $group: {
        _id: 'mobile',
        calls: { $sum: '$calls' }
    }
 })

but even after referring to these questions & tutorial, its equivalent Java code...

Aggregation agg = Aggregation.newAggregation(Aggregation.match(Criteria.where("mobile").is("1234567890"),
    Aggregation.group("mobile").sum("calls").as("totalCalls"),
    Aggregation.project("totalCalls"));
AggregationResults<Doc> results = mongoTemplate.aggregate(agg, "yoCollection",
    Doc.class);
Doc doc = results.getMappedResults().get(0);

...returns an empty list & throws IndexOutOfBoundsException though my query returns results on console!

Community
  • 1
  • 1
Parth
  • 729
  • 8
  • 23

1 Answers1

4

You are missing a closing parenthesis for the match() parameter:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

Aggregation agg = Aggregation.newAggregation(
        match(Criteria.where("mobile").is("1234567890")), // <-- missing closing parenthesis
        group("mobile").sum("calls").as("totalCalls"),
        project("totalCalls")
    );

AggregationResults<Doc> results = mongoTemplate.aggregate(agg, "yoCollection", Doc.class);
Doc doc = results.getMappedResults().get(0);
Gismo Ranas
  • 6,043
  • 3
  • 27
  • 39
chridam
  • 100,957
  • 23
  • 236
  • 235
  • After reformatting long chained methods, it's working! Thanks, maybe missing closing parenthesis was the problem, but I wonder why eclipse didn't warn me :) – Parth Oct 28 '15 at 18:11