반응형
우선 몽고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 |
반응형
'DB > MongoDB' 카테고리의 다른 글
[MongoDB] 몽고DB Index 생성 및 이용 (0) | 2020.09.16 |
---|---|
[mongo] 배열 데이터 삭제 (0) | 2020.09.16 |
mongoDB 에 json 값 저장하기 ( spring ) (0) | 2020.01.06 |
Multiple MongoDB connections with Spring Boot (0) | 2019.02.22 |
[mongodb] spring boot 연동 (0) | 2019.02.22 |
댓글