使用下面的聚合,我试图找出聊天机器人或Agent在办公室和非办公室时间解决的对话数量。通过下面的聚合,我得到了一堆文档,它们在solved和horario上存在差异,0表示离开办公室,1表示办公室谈话。我目前的结果如下:
我如何才能得到以下结果,它根据solved和horario将所有文档相加:
solved: Clipbot
horario: 0
count: 300
和
solved: Clipbot
horario: 1
count: 320
和
solved: Agent
horario: 0
count: 200
和
solved: Agent
horario: 1
count: 320
我当前的聚合:
[{$match: {
start_date: {
$gte: ISODate('2020-06-01 00:00:01'),
$lte: ISODate('2020-06-30 23:59:59')
}
}}, {$project: {
solved: "$solved",
hour: {
$hour: {
date: '$start_date',
timezone: 'America/Mexico_City'
}
}
}}, {$project: {
solved: "$solved",
horario: {
$cond: [
{
$and: [
{
$gt: [
'$hour',
7
]
},
{
$lt: [
'$hour',
22
]
},
]
},
1,
0
]
}
}}]
发布于 2020-07-24 23:48:35
您可以使用$group
,我还没有添加您在查询中添加的其他字段,
db.collection.aggregate([
// you can add your $match conditions here
{
$group: {
_id: {
solved: "$solved",
horario: "$horario"
},
count: { $sum: 1 },
solved: { $first: "$solved" },
horario: { $first: "$horario" }
}
},
{
$project: { _id: 0 }
}
])
https://stackoverflow.com/questions/63062192
复制相似问题