8

I started to use MongoDB database in my application and for data access I have chosen Spring Data for MongoDB.

I skimmed API reference and documentation and I can see that there is map-reduce integration but what about aggregation framework? I can see that it supports group by operation, which would indicate that it supports $group operator judging from this: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/, but what about other operators, are that not supported for now?

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

4 Answers 4

8

Spring Data 1.3.0.RC1 is available and it does support the aggregation framework.

For example: The shell aggregation comand:

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

is run like this 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);

The documentation is here

NOTE: We recently had to switch to using the BUILD-SNAPSHOT build of version 1.3.0. This change necessitated the change to 2 of the above lines which have changed to:

AggregationOperation group = Aggregation.group("card_acceptor").sum("amount").as("amount_sum").count().as("tran_count");
Aggregation aggregation = Aggregation.newAggregation(match, group);
Sign up to request clarification or add additional context in comments.

1 Comment

5

The Spring Data MongoOperations.group() method is mapped to db.collection.group() MongoDB command and not the $group aggregation function. Currently there is no support in Spring Data MongoDB for aggregation framework. Map reduce, as you have mentioned, is supported though

1 Comment

You can, however, take one step down to the mongodb Java driver whenever you need powers of aggregation framework. mongoOps.getCollection("yourCollection").aggregate( ... )
1
 Aggregation aggregation = newAggregation(
        match(Criteria.where("salesyear").is(year)),
        group("brand","salesyear").sum("numberOfCars").as("total"),
        sort(Sort.Direction.ASC, previousOperation(), "brand")    
      );

Comments

0

Here is how to get the sum of a particular 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;

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.