mongoDB sum数据(功能类似于mysql中 sum()、case、when)
示例代码:
// hymCollect 具体集合(类似于mysql表)
db.hymCollect.aggregate(
{// 条件
$match : {bookDate:{$gte:new Date("2021-06-11 16:00:00"),$lt:new Date("2021-06-14 16:00:00")}}
},
{$group:{
_id: 0,// 总数
'总预约数':{$sum:1},
'省内12号预约' : { $sum : { $cond: [// 多条件case
{$and: [
{$gte: ["$bookDate", new Date("2021-06-11 16:00:00")]},
{$lt :["$bookDate", new Date("2021-06-12 16:00:00")]},
{$eq:["$status", 'NORMAL']},
{$eq:["$province", '湖北']}
]
},
1, 0 ] } },
'省外12号预约' : { $sum : { $cond: [
{$and: [
{$gte: ["$bookDate", new Date("2021-06-11 16:00:00")]},
{$lt :["$bookDate", new Date("2021-06-12 16:00:00")]},
{$eq:["$status", 'NORMAL']},
{$ne:["$province", '湖北']}
]
},
1, 0 ] } }
}});