網站首頁 編程語言 正文
1、前言
在上一章的學習中,我們知道了Spring Data MongoDB的基本用法,但是對于一些聚合操作,還是不熟悉的,所以本博客介紹一些常用的聚合函數
2、什么是聚合?
MongoDB 中使用聚合(Aggregations)來分析數據并從中獲取有意義的信息。在這個過程,一個階段的輸出作為輸入傳遞到下一個階段
常用的聚合函數
聚合函數 | SQL類比 | 描述 |
---|---|---|
project | SELECT | 類似于select關鍵字,篩選出對應字段 |
match | WHERE | 類似于sql中的where,進行條件篩選 |
group | GROUP BY | 進行group by分組操作 |
sort | ORDER BY | 對應字段進行排序 |
count | COUNT | 統計計數,類似于sql中的count |
limit | LIMIT | 限制返回的數據,一般用于分頁 |
out | SELECT INTO NEW_TABLE | 將查詢出來的數據,放在另外一個document(Table) , 會在MongoDB數據庫生成一個新的表 |
3、環境搭建
- 開發環境
- JDK 1.8
- SpringBoot2.2.1
- Maven 3.2+
- 開發工具
- IntelliJ IDEA
- smartGit
- Navicat15
使用阿里云提供的腳手架快速創建項目:
https://start.aliyun.com/bootstrap.html
也可以在idea里,將這個鏈接復制到Spring Initializr這里,然后創建項目
jdk選擇8的
選擇spring data MongoDB
4、數據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、例子應用
本博客,不每一個函數都介紹,通過一些聚合函數配置使用的例子,加深讀者的理解
統計用戶名為User1的用戶數量
@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); } }
對用戶名進行分組,避免重復,group
第一個參數$name
類似于group by name
,調用Accumulators
的sum
函數,其實類似于SQL,SELECT name ,sum(1) as sumnum FROM
usergroup 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); } }
參考資料
原文鏈接:https://blog.csdn.net/u014427391/article/details/122828360
相關推薦
- 2023-07-16 oracle 加解密函數
- 2022-08-15 當添加一個鍵值對元素時,HashMap發生了什么?
- 2022-06-29 python循環神經網絡RNN函數tf.nn.dynamic_rnn使用_python
- 2021-12-09 android藍牙簡單開發示例教程_Android
- 2022-10-11 Android之InstanceState詳解
- 2023-01-18 React報錯Element?type?is?invalid解決案例_React
- 2022-03-22 C++名稱空間特性_C 語言
- 2022-06-01 Android利用MediaRecorder實現錄音功能_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支