2015-02-06 31 views
1

我想用Spring Data运行这个简单的查询。用弹簧数据运行简单的聚合查询

db.collectionname.aggregate(
    { $group: { 
     _id: { group_name: "$col1" }, 
     'total_sum': { $sum: 1 } 
    }} 
); 

我经历了几篇关于SO的文章,但无法找到执行此查询的方法。是否有可能使用SpringData运行此查询,然后如何?

回答

0

它是象下面这样:

Aggregation aggregation = newAggregation(
       group("col1").count().as(
         "count")); 
AggregationResults<DBObject> aggregate = mongoTemplate.aggregate(
       aggregation, CollectionClass.class, DBObject.class); 
     List<DBObject> mappedResults = aggregate.getMappedResults();