因此,我在路线页面上有以下查询:
const testeEa = atendimentos.aggregate([
{$group : {_id: "$id_atendente", Idcount:{$sum:1}}},
{$sort: {_id: 1}},
{ '$group': {
'_id': null,
'eatest': {
'$sum': {
'$cond' : [ { '$eq': ['$status', 'EA'] }, 1, 0]
}
},
//'eatest': {'$push': "$$ROOT"}
} }
]).exec();
我想做的是:此IDcount正在计算id_atendente重复的次数。我需要检查每个人回答了多少个支持电话。
完成此操作后,我需要检查所有状态为“ EA”的支持电话。
我有351个处于“ EA”状态的呼叫,我想在支持呼叫中查看谁处于此状态。
我想我在第二个$ group上缺少了什么,我只是不知道它是什么。
这个吃东西应该是将在视图上使用的键。
顺便说一句,我设法进行了查询,可以获取每个ID的支持电话数量,我几乎需要做同样的事情,不同之处在于,我只需要具有“ EA”状态的支持电话。
编辑1
const counts = atendimentos.aggregate([
{ '$group': {
'_id': null,
'fin': {
'$sum': {
'$cond': [ { '$eq': [ '$status', 'F' ] }, 1, 0 ]
}
},
'ea': {
'$sum': {
'$cond': [ { '$eq': [ '$status', 'EA' ] }, 1, 0 ]
}
}
} }
]).exec()
//Faz uma consulta no mongo e guarda o resultado
//na variável monthly
const monthly = atendimentos.aggregate([
{ '$group': {
'_id': {
'year': { '$year': '$date' },
'month': { '$month': '$date' }
},
'sum': { '$sum': 1 }
} },
{ '$group': {
'_id': null,
//Chave usada para renderizar os dados
'back': { '$push': '$$ROOT' }
} },
]).exec();
//Verificar quantas vezes um id_atendente se repete, contar e guardar o numero
const testeAt = atendimentos.aggregate([
{$group : {_id: "$id_atendente", Idcount:{$sum:1}}},
{$sort: {_id: 1}},
{ '$group': {
'_id': null,
//Chave usada para renderizar os dados
'test': {'$push': "$$ROOT"}
} },
]).exec();
const atendente = atendimentos.aggregate([
{ '$group' : {
'_id': "$id_atendente",
'Idcount': { '$sum': 1 }
} },
{ '$sort': { '_id': 1 } }
]).exec();
const testeEa = atendimentos.aggregate([
{ '$group': {
'_id': null,
'eatest': {
'$sum': {
'$cond' : [ { '$eq': ['$status', 'EA'] }, 1, 0]
}
}
} }
]).exec();
Promise.all([counts, monthly, testeAt, testeEa]).then(([counts, monthly, testeAt, testeEa]) => {
请注意,atendente查询和testeAt几乎相同。
我想做的是使用这个testeEa变量来存储查询的返回值,这些查询返回每个id_atendente的“ EA”状态数。
如果我使用try catch,我猜是做不到的,因为testeEa会在其中,并且我无法将其传递给我的数组。
最有趣的是,顺便说一句返回正确的值。
编辑结束
编辑2
我想要的数据的一个示例,它是用于检查呼叫/ id的查询。
{
"_id": 42,
"Idcount": 3
},
{
"_id": 43,
"Idcount": 155
},
{
"_id": 46,
"Idcount": 69
},
{
"_id": 47,
"Idcount": 16
},
{
"_id": 48,
"Idcount": 4
},
{
"_id": 49,
"Idcount": 21
},
{
"_id": 50,
"Idcount": 4
},
这正是我想要的方式,但是不同的是,我只需要具有'EA'身份的人。
Idcount是具有“ EA”状态的ID出现的次数。
编辑结束2
谢谢!
答案 0 :(得分:0)
在聚合框架中执行管道时,MongoDB通过管道将运算符相互传递。 这里的“管道”具有Linux的含义:运算符的输出将成为以下运算符的输入。每个运算符的结果都是新的文档集合。 因此,当Mongo执行上述管道时,前两个步骤的结果
{ '$group' : { /* First pipeline step */
'_id': "$id_atendente",
'Idcount': { '$sum': 1 }
} },
{ '$sort': { '_id': 1 } } /* Second pipeline step */
将是具有模式的文档数组(例如):
[
{ _id: 'fuzz', IdCount: 2 },
{ _id: 'foo', IdCount: 9 },
{ _id: 'bar', IdCount: 4 },
....
]
现在执行第三个管道
{ '$group': {
'_id': null,
'eatest': {
'$sum': {
'$cond' : [ { '$eq': ['$status', 'EA'] }, 1, 0]
}
}
} }
前一个管道中的文档被管道传输到该管道中,期望文档具有称为status的字段,因此该字段不存在 结果将不正确。
您需要并行运行多个聚合管道,这只能通过 $facet
的单个查询来实现:
atendimentos.aggregate([
{ '$facet': {
'atendente': [
{ '$group' : {
'_id': "$id_atendente",
'Idcount': { '$sum': 1 }
} },
{ '$sort': { '_id': 1 } }
],
'eatest': [
{ '$group': {
'_id': null,
'eatest': {
'$sum': {
'$cond' : [ { '$eq': ['$status', 'EA'] }, 1, 0]
}
}
} }
]
} }
]).exec((err, result) => console.log(result));
(async () => {
try {
const atendente = await atendimentos.aggregate([
{ '$group' : {
'_id': "$id_atendente",
'Idcount': { '$sum': 1 }
} },
{ '$sort': { '_id': 1 } }
]).exec();
const eatest = await atendimentos.aggregate([
{ '$group': {
'_id': null,
'eatest': {
'$sum': {
'$cond' : [ { '$eq': ['$status', 'EA'] }, 1, 0]
}
}
} }
]).exec();
const data = { atendente, eatest };
console.log(JSON.stringify(data, null, 4));
} catch (err) {
console.error(err);
}
})();
或使用Promise API
(() => {
const atendente = atendimentos.aggregate([
{ '$group' : {
'_id': "$id_atendente",
'Idcount': { '$sum': 1 }
} },
{ '$sort': { '_id': 1 } }
]).exec();
const eatest = atendimentos.aggregate([
{ '$group': {
'_id': null,
'eatest': {
'$sum': {
'$cond' : [ { '$eq': ['$status', 'EA'] }, 1, 0]
}
}
} }
]).exec();
Promise.all([atendente, eatest]).then(([ atendente, eatest ]) => {
const data = { atendente, eatest };
console.log(JSON.stringify(data, null, 4));
}).catch((err) => {
console.error(err);
});
})();