[MongoDB] 특정 필드의 합 구하기

Yuri Lee·2022년 7월 20일
0

Intro

MongoDB에서 특정 필드의 합을 구할 수 있는 shell script 에 대해 알아보도록 하자. 참고로 studio 3t 를 사용했다.

How to do

db = db.getSiblingDB("DB_Name");

total = 0
db.getCollection("Collection_name").find(
    {
        "id": NumberInt(10021),
        "createdDate": {
            "$gte": "20220601",
            "$lte": "20220630"
        }
    }
).forEach(function (doc) {
    total += doc.total
});

printjson(total)
profile
Step by step goes a long way ✨

0개의 댓글