Spring data mongodb - aggregation structure integration

I started using the MongoDB database in my application and I selected Spring Data for MongoDB to access the data.

I looked through the API link and the documentation, and I see that there is integration with map reduction, but what is the aggregation structure? I see that it supports a group operation, which indicates that it supports the $group operator, judging by this: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/ , but as for the others operators not yet supported?

I ask this question because I wanted to know what kind of integration with Sping Data MongoDB data, so I know what to expect, so to speak.

+8
spring-data mongodb aggregation-framework
source share
4 answers

The Spring Data MongoOperations.group() method maps to the db.collection.group() MongoDB command, not the $group aggregation function. There is currently no support for Spring Data MongoDB for the aggregation structure. Map abbreviation, as you already mentioned, is supported, although

+5
source share

Spring Data 1.3.0.RC1 is available, and it supports an aggregation structure.

For example: Shell Aggregation Combination:

 db.eft_transactions.aggregate( {$match: { service:"EFT", source:"MARKUP", } }, {$group: { _id:"$card_acceptor_id", tran_count:{$sum:1}, amount_sum:{$sum:"$amount"} } } ) 

runs as follows from java:

  AggregationOperation match = Aggregation.match(Criteria.where("service").is("EFT").and("source").is("MARKUP")); AggregationOperation group = Aggregation.group("card_acceptor").and("amount_sum").sum("amount").and("tran_count").count(); Aggregation aggregation = newAggregation(match, group); AggregationResults<StoreSummary> result = this.mongoTemplate.aggregate(aggregation, "eft_transactions", StoreSummary.class); 

Documentation here

NOTE. Recently we had to switch to using the BUILD-SNAPSHOT build version 1.3.0. This change required a change to the two above lines, which changed to:

 AggregationOperation group = Aggregation.group("card_acceptor").sum("amount").as("amount_sum").count().as("tran_count"); Aggregation aggregation = Aggregation.newAggregation(match, group); 
+8
source share
  Aggregation aggregation = newAggregation( match(Criteria.where("salesyear").is(year)), group("brand","salesyear").sum("numberOfCars").as("total"), sort(Sort.Direction.ASC, previousOperation(), "brand") ); 
+1
source share

Here's how to get the sum of a specific field.

 private Map<String, Long> getTotalMap(){ /* db.pDSSummaryModel.aggregate([{ $group: { _id: null, total: { $sum: '$totalUniqueCustomerCount' } } }]) */ Aggregation aggregations = newAggregation( group("null").sum("totalUniqueUserCount").as("userTotal") .sum("totalUniqueCustomerCount").as("customerTotal"), project("customerTotal", "userTotal") ); AggregationResults<DBObject> results = mongoTemplate.aggregate(aggregations, "pDSSummaryModel", DBObject.class); List<DBObject> fieldList = results.getMappedResults(); Map<String, Long> map = new HashMap<>(); if(fieldList != null && !fieldList.isEmpty()) { for(DBObject db: fieldList){ map.put("userTotal", parseLong(db.get("userTotal").toString())); map.put("customerTotal", parseLong(db.get("customerTotal").toString())); } } return map; } 
0
source share

All Articles