DB/MongoDB

[studio 3t] mongoDB group by 하는 법 , count 방법

벨포트조던 2019. 10. 15.
반응형

우선 몽고DB를 제대로 공부하지 않아서 대충 검색햇던 지식밖에없다. 

 

그래서 다 검색하고 할려니까 studio3t에서 조건절만 걸어봤지 group 은 안해봐서 모르겠더라 .

 

우선 답은 Aggregation  이거다

 

사용법은

https://studio3t.com/knowledge-base/articles/build-mongodb-aggregation-queries/

공식 문서를 참조 .

 

 

내가 필요한건 조건에 맞는 count 를 하는거였음.

 

rdb는 기본이니 쉬운데... 몽고는 잘몰르것어서 검색으로 찾음

 

https://stackoverflow.com/questions/34826670/how-to-do-having-count-in-mongodb?rq=1

 

 

 

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
db.collection.aggregate([{
  '$group': {
    '_id': {'author''$author''subreddit''$subreddit'}, 
    'count': {'$sum'1}, 
    'data': {'$addToSet''$$ROOT'}}
}, {
  '$match': {
    'count': {'$eq'1}
}}])
 
 
 
 
//Where data is one-length list with matched document.
 
//if you want to get some exact field, it should look like this:
 
 
 
db.collection.aggregate([{
  '$group': {
    '_id': {'author''$author''subreddit''$subreddit'}, 
    'count': {'$sum'1}, 
    'author': {'$last''$author'}}
}, {
  '$match': {
    'count': {'$eq'1}
}}])
cs
반응형

댓글