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!
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With