这篇文章主要介绍下在data框架中如何使用Aggregation进行分组统计。
基本的操作包括:
基于我们之前文章的数据我们就简单的统计下每个用户发过多少篇文章。 如果是用的mysql那么查询语句如下:
select article.author, count(*) as count from article_info as article group by article.author having count > 0
既然我们要用mongodb来实现,还要用框架来做,那么首先我们得知道用原始的语句怎么写,如下:
db.article_info.aggregate([
{
"$group": {
"_id": "$author",
"count": {
"$sum": 1
},
"name": {
"$first": "$author"
}
}
},
{
"$project": {
"name": 1,
"count": 1,
"_id": 0
}
},
{
"$match": {
"count": {
"$gt": 0
}
}
}
]);
$group:根据author分组,然后统计次数,用$sum函数,显示第一个名称 $project:定义要显示的key,1为显示,0为不显示 $match:过滤掉没发过文章的用户,次数大于0
下面看spring-data-mongodb中我们要怎么去实现这个功能
Aggregation agg = newAggregation(
group("author").count().as("count").first("author").as("name"),
project("name","count"),
sort(Direction.DESC, "count"),
match(Criteria.where("count").gt(0))
);
AggregationResults<ArticleResult> results = mongoTemplate.aggregate(agg, "article_info", ArticleResult.class);
List<ArticleResult> tagCount = results.getMappedResults();
for (ArticleResult studentResult : tagCount) {
System.out.println(studentResult.getName() + "\t" + studentResult.getCount());
}
按照原始的语句,用框架的语法拼出来就可以了
当然还有的小伙伴还是用习惯了java驱动的写法,也可以,就是没上面简洁
List<DBObject> pipeline = new ArrayList<DBObject>();
BasicDBObject group = new BasicDBObject();
group.put("$group", new BasicDBObject("_id","$author")
.append("count", new BasicDBObject("$sum",1))
.append("name", new BasicDBObject("$first","$author")));
BasicDBObject project = new BasicDBObject();
project.put("$project", new BasicDBObject("name",1)
.append("count", 1).append("_id", 0));
pipeline.add(group);
pipeline.add(project);
AggregationOutput output = mongoTemplate.getCollection("article_info").aggregate(pipeline);
Iterable<DBObject> iterable = output.results();
for (DBObject dbObject : iterable) {
System.out.println(dbObject);
}