网站首页 > 基础教程 正文
这篇文章主要介绍下在data框架中如何使用Aggregation进行分组统计。
今天是国庆节,祝大家节日快乐。
基本的操作包括:
$project - 可以从子文档中提取字段,可以重命名字段。
$match - 可以实现查找的功能。
$limit - 接受一个数字n,返回结果集中的前n个文档。
$skip - 接受一个数字n,丢弃结果集中的前n个文档。
$group - 统计操作, 还提供了一系列子命令。
$avg, $sum 等等函数…。
$sort - 排序。
基于我们之前文章的数据我们就简单的统计下每个用户发过多少篇文章。 如果是用的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中我们要怎么去实现这个功能
/**
* 聚合使用
* 统计每个用户的文章数量
*/
private static void aggregation() {
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驱动的写法,也可以,就是没上面简洁
private static void aggreate() {
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);
}
}
源码地址:https://github.com/yinjihuan/cxytiandi
猜你喜欢
- 2024-10-29 57个挑战之57(part6):客户端+web前端+服务端代码实现
- 2024-10-29 技术干货|MongoDB数据库常见操作命令
- 2024-10-29 ABP vNext框架文档解读28-数据过滤
- 2024-10-29 自建MongoDB实践:MongoDB 分片集群
- 2024-10-29 小程序 随机读取数据并生成分享图片 上手笔记
- 2024-10-29 go-mongox:简单高效,让文档操作和 bson 数据构造更流畅
- 2024-10-29 当MongoDB遇见Spark mongodb campass
- 2024-10-29 MongoDB 5.0 官方文档学习笔记 mongodb教程
- 2024-10-29 好东西,MySQL 数据库 MongoDB详解
- 2024-10-29 MongoDB 入门 day04 mongodb27017
- 最近发表
- 标签列表
-
- jsp (69)
- pythonlist (60)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- mysql教程 (60)
- pythonif (86)
- location.href (69)
- deletesql (62)
- c++模板 (62)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- console.table (62)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)