日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

SpringBoot系列之MongoDB?Aggregations用法詳解_MongoDB

作者:smileNicky ? 更新時間: 2022-04-15 編程語言

1、前言

上一章的學習中,我們知道了Spring Data MongoDB的基本用法,但是對于一些聚合操作,還是不熟悉的,所以本博客介紹一些常用的聚合函數(shù)

2、什么是聚合?

MongoDB 中使用聚合(Aggregations)來分析數(shù)據(jù)并從中獲取有意義的信息。在這個過程,一個階段的輸出作為輸入傳遞到下一個階段

常用的聚合函數(shù)

聚合函數(shù) SQL類比 描述
project SELECT 類似于select關(guān)鍵字,篩選出對應字段
match WHERE 類似于sql中的where,進行條件篩選
group GROUP BY 進行g(shù)roup by分組操作
sort ORDER BY 對應字段進行排序
count COUNT 統(tǒng)計計數(shù),類似于sql中的count
limit LIMIT 限制返回的數(shù)據(jù),一般用于分頁
out SELECT INTO NEW_TABLE 將查詢出來的數(shù)據(jù),放在另外一個document(Table) , 會在MongoDB數(shù)據(jù)庫生成一個新的表

3、環(huán)境搭建

  • 開發(fā)環(huán)境
  • JDK 1.8
  • SpringBoot2.2.1
  • Maven 3.2+
  • 開發(fā)工具
  • IntelliJ IDEA
  • smartGit
  • Navicat15

使用阿里云提供的腳手架快速創(chuàng)建項目:
https://start.aliyun.com/bootstrap.html

也可以在idea里,將這個鏈接復制到Spring Initializr這里,然后創(chuà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、例子應用

本博客,不每一個函數(shù)都介紹,通過一些聚合函數(shù)配置使用的例子,加深讀者的理解

統(tǒng)計用戶名為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);
      }

  }

對用戶名進行分組,避免重復,group第一個參數(shù)$name類似于group by name,調(diào)用Accumulatorssum函數(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

原文鏈接:https://blog.csdn.net/u014427391/article/details/122828360

欄目分類
最近更新