SpringBoot系列之MongoDB?Aggregations用法詳解
1、前言
在上一章的學(xué)習(xí)中,我們知道了Spring Data MongoDB的基本用法,但是對(duì)于一些聚合操作,還是不熟悉的,所以本博客介紹一些常用的聚合函數(shù)
2、什么是聚合?
MongoDB 中使用聚合(Aggregations)來分析數(shù)據(jù)并從中獲取有意義的信息。在這個(gè)過程,一個(gè)階段的輸出作為輸入傳遞到下一個(gè)階段
常用的聚合函數(shù)
| 聚合函數(shù) | SQL類比 | 描述 |
|---|---|---|
| project | SELECT | 類似于select關(guān)鍵字,篩選出對(duì)應(yīng)字段 |
| match | WHERE | 類似于sql中的where,進(jìn)行條件篩選 |
| group | GROUP BY | 進(jìn)行g(shù)roup by分組操作 |
| sort | ORDER BY | 對(duì)應(yīng)字段進(jìn)行排序 |
| count | COUNT | 統(tǒng)計(jì)計(jì)數(shù),類似于sql中的count |
| limit | LIMIT | 限制返回的數(shù)據(jù),一般用于分頁(yè) |
| out | SELECT INTO NEW_TABLE | 將查詢出來的數(shù)據(jù),放在另外一個(gè)document(Table) , 會(huì)在MongoDB數(shù)據(jù)庫(kù)生成一個(gè)新的表 |
3、環(huán)境搭建
- 開發(fā)環(huán)境
- JDK 1.8
- SpringBoot2.2.1
- Maven 3.2+
- 開發(fā)工具
- IntelliJ IDEA
- smartGit
- Navicat15
使用阿里云提供的腳手架快速創(chuàng)建項(xiàng)目:
https://start.aliyun.com/bootstrap.html

也可以在idea里,將這個(gè)鏈接復(fù)制到Spring Initializr這里,然后創(chuàng)建項(xiàng)目

jdk選擇8的

選擇spring data MongoDB

4、數(shù)據(jù)initialize
private static final String DATABASE = "test";
private static final String COLLECTION = "user";
private static final String USER_JSON = "/userjson.txt";
private static MongoClient mongoClient;
private static MongoDatabase mongoDatabase;
private static MongoCollection<Document> collection;
@BeforeClass
public static void init() throws IOException {
mongoClient = new MongoClient("192.168.0.61", 27017);
mongoDatabase = mongoClient.getDatabase(DATABASE);
collection = mongoDatabase.getCollection(COLLECTION);
collection.drop();
InputStream inputStream = MongodbAggregationTests.class.getResourceAsStream(USER_JSON);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
reader.lines()
.forEach(l->collection.insertOne(Document.parse(l)));
reader.close();
}5、例子應(yīng)用
本博客,不每一個(gè)函數(shù)都介紹,通過一些聚合函數(shù)配置使用的例子,加深讀者的理解
統(tǒng)計(jì)用戶名為User1的用戶數(shù)量
@Test
public void matchCountTest() {
Document first = collection.aggregate(
Arrays.asList(match(Filters.eq("name", "User1")), count()))
.first();
log.info("count:{}" , first.get("count"));
assertEquals(1 , first.get("count"));
}skip跳過記錄,只查看后面5條記錄
@Test
public void skipTest() {
AggregateIterable<Document> iterable = collection.aggregate(Arrays.asList(skip(5)));
for (Document next : iterable) {
log.info("user:{}" ,next);
}
}對(duì)用戶名進(jìn)行分組,避免重復(fù),group第一個(gè)參數(shù)$name類似于group by name,調(diào)用Accumulators的sum函數(shù),其實(shí)類似于SQL,SELECT name ,sum(1) as sumnum FROMusergroup by name
@Test
public void groupTest() {
AggregateIterable<Document> iterable = collection.aggregate(Arrays.asList(
group("$name" , Accumulators.sum("sumnum" , 1)),
sort(Sorts.ascending("_id"))
));
for (Document next : iterable) {
log.info("user:{}" ,next);
}
}參考資料
MongoDB 聚合 https://www.runoob.com/
MongoDB Aggregations Using Java
到此這篇關(guān)于SpringBoot系列之MongoDB Aggregations用法的文章就介紹到這了,更多相關(guān)SpringBoot MongoDB Aggregations用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mongodb索引知識(shí)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章給大家介紹了mongodb索引的建立,刪除索引的方法以及唯一索引和組合索引的知識(shí),感興趣的朋友一起看看吧2017-08-08
CentOS 6.4創(chuàng)建Mongodb副本集
這篇文章主要為大家詳細(xì)介紹了CentOS 6.4創(chuàng)建Mongodb副本集的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
SpringBoot整合MongoDB的實(shí)現(xiàn)步驟
MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫(kù)當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫(kù)的。本文介紹SpringBoot項(xiàng)目如何整合MongoDB2021-06-06
Mongodb 利用mongoshell進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Mongodb 利用mongoshell進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
centos yum 安裝 mongodb 以及php擴(kuò)展
MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由C++語(yǔ)言編寫。旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。2014-07-07
mongodb監(jiān)控工具mongostat的使用及命令詳解
mongostat是mongodb自帶的狀態(tài)檢測(cè)工具,在命令行下使用,會(huì)間隔固定時(shí)間獲取mongodb的當(dāng)前運(yùn)行狀態(tài),并輸出,本文講述了mongodb監(jiān)控工具mongostat的使用及命令詳解2018-03-03

