본문 바로가기
[ Developer ]/MongoDB

[MongoDB] 몽고디비 스프링 Aggregation Group

by 김현섭. 2016. 7. 4.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Spring-Data-MongoDB Aggregation Group

Aggregation Group을 하기 위해서 이전에 했던 방식과 비슷하게 작성을 한다
GroupOperation으로 group를 추가하고 결과를 받아온다


이제는 main에서 gropData로 시작을 해주면 된다


결과는 다음과 같이 나온다


*Group - Main
1
2
3
4
5
6
7
8
    public static void main(String[] args) {
        AggregationTest mongoTest = new AggregationTest();
        //mongoTest.findData("name", "둘리");
        //mongoTest.pagingData("name", "둘리", 1);
        
        mongoTest.groupData();
        
    }
cs

*Group - Method
1
2
3
4
5
6
7
8
9
10
11
12
13
    private void groupData() {
        
        GroupOperation group = Aggregation.group("name").count().as("cnt");
        Aggregation aggregation = Aggregation.newAggregation(group);
        
        AggregationResults results = mongoTemplate.aggregate(aggregation, "person", Map.class);
        
        List<Map> list = results.getMappedResults();
        
        for (Map map : list) {
            System.out.println(map);
        }
    }
cs


* Aggregation Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
public class AggregationTest {
    
    private MongoTemplate mongoTemplate;
    
    public AggregationTest() {
        
        String mongoContextPath = "/mongoContext.xml";
        
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(mongoContextPath);
        
        mongoTemplate = (MongoTemplate) ctx.getBean("mongoTemplate");
    }
    
    public static void main(String[] args) {
        AggregationTest mongoTest = new AggregationTest();
        
        // Aggregation Sort, Skip, Limit
        //mongoTest.findData("name", "도우너");
        
        // Aggregation Sort, Skip, Limit을 이용한 Paging
        //mongoTest.pagingData("name", "^둘리", 1);
        
        // Aggregation Group
        mongoTest.groupData();
    }
    
     public void findData(String key, String value) {
          
          Criteria criteria = new Criteria(key);
          criteria.is(value);
          
          // 검색하기
          MatchOperation match = Aggregation.match(criteria);
          
          // 정렬하기
          SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "_id");
          
          // 건너뛰기
          SkipOperation skip = Aggregation.skip(2);
          
          // 지정한 수 만큼 가져오기
          LimitOperation limit = Aggregation.limit(4);
          
          Aggregation aggregation = Aggregation.newAggregation(match,sort,skip,limit);
          
          AggregationResults<MongoTestVO> result = mongoTemplate.aggregate(aggregation, "person", MongoTestVO.class);
          
          List<MongoTestVO> dataList = result.getMappedResults();
          
          for (MongoTestVO mongoTestVO : dataList) {
             System.out.println(mongoTestVO.getName());
             System.out.println(mongoTestVO.getAddress());
             System.out.println(mongoTestVO.getId());
          }
   }
 
    private void pagingData(String key, String value, int pageNo) {
        
        Criteria criteria = new Criteria(key);
        criteria.regex(value);
        
        MatchOperation match = Aggregation.match(criteria);
        
        // 정렬
        SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "_id");
        
        // 건너뛰기
        SkipOperation skip = Aggregation.skip((pageNo - 1* 10);
        
        // 지정한 수 만큼 가져오기
        LimitOperation limit = Aggregation.limit(10);
        
        Aggregation aggregation = Aggregation.newAggregation(match, sort, skip, limit);
        
        AggregationResults<MongoTestVO> result = mongoTemplate.aggregate(aggregation, "person", MongoTestVO.class);
        
        List<MongoTestVO> dataList = result.getMappedResults();
        
        for (MongoTestVO mongoTestVO : dataList) {
            System.out.println("ID " + mongoTestVO.getId());
            System.out.println("Name " + mongoTestVO.getName());
            System.out.println("Address " + mongoTestVO.getAddress());
        }
    }
    
    private void groupData() {
        
        GroupOperation group = Aggregation.group("name").count().as("cnt");
        Aggregation aggregation = Aggregation.newAggregation(group);
        
        AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "person", Map.class);
        
        List<Map> list = results.getMappedResults();
        
        for (Map map : list) {
            System.out.println(map);
        }
    }
    
    @Document(collection="person")
    private static class MongoTestVO {
        
        @Id
        private String id;
        
        private String name;
        private String address;
        
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
    }
}
cs