mongodb 聚合案例分享
程序员文章站
2022-05-30 15:05:15
...
聚合案例
db.getCollection("hisDetail_newdate").aggregate([
{
$match: {
"AC_DT": {
$gte: "20190324",
$lte: "20190401"
}
}
},
{
$group: {
"_id": {"LOG_NO":"$LOG_NO","AC_DT":"$AC_DT"} , // _id 固定写法.多个字段分组
"TXN_AMT": {
$sum: 1
}
}
},
{
$sort: { // 多个字段排序
"TXN_AMT": 1,
"SORT_ID":-1
}
}
])
对某个字段汇总
db.getCollection("ACCOUNTSEQ").aggregate([
{
$match: {
"CUSTID" : "2015030300842100",
"ACCOUNTDATE":{$gte:"20180101",$lt:"20190101"}
}
},
{
$group: {
"_id":null , // _id 固定写法.多个字段分组
"CASH_AMOUNT": {
$sum: "$CASH_AMOUNT"
},
"UNCASH_AMOUNT":{
$sum: "$UNCASH_AMOUNT"
},
"CREDIT_AMOUNT":{
$sum: "$CREDIT_AMOUNT"
}
}
}
])