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