mongodb当前表字段 sendOrgCode、sendStatus、mobile, 要求根据sendStatus统计出所有sendOrgCode的发送条数、其中包含 成功条数、失败条数、总条数
时间: 2023-04-10 18:02:46 浏览: 43
可以使用 MongoDB 的聚合框架来实现这个需求。具体的聚合管道如下:
```
db.collection.aggregate([
// 匹配 sendStatus 为成功或失败的文档
{ $match: { sendStatus: { $in: ["成功", "失败"] } } },
// 按 sendOrgCode 和 sendStatus 分组,统计条数
{ $group: {
_id: { sendOrgCode: "$sendOrgCode", sendStatus: "$sendStatus" },
count: { $sum: 1 }
} },
// 将结果按 sendOrgCode 分组,生成统计报表
{ $group: {
_id: "$_id.sendOrgCode",
totalCount: { $sum: "$count" },
successCount: {
$sum: { $cond: [ { $eq: [ "$_id.sendStatus", "成功" ] }, "$count", 0 ] }
},
failCount: {
$sum: { $cond: [ { $eq: [ "$_id.sendStatus", "失败" ] }, "$count", 0 ] }
}
} }
])
```
这个聚合管道会先匹配 sendStatus 为成功或失败的文档,然后按 sendOrgCode 和 sendStatus 分组,统计条数。最后再按 sendOrgCode 分组,生成统计报表,包括总条数、成功条数和失败条数。