Node.js 将数据从聚合拆分为2个数组
我有以下模式: const mongoose=require('mongoose') 我计算每个Node.js 将数据从聚合拆分为2个数组,node.js,mongoose,Node.js,Mongoose,我有以下模式: const mongoose=require('mongoose') 我计算每个disconnectType的文档数,并将其作为数组返回。 正如您在模型模式中看到的,这些是disconnectTypes: “调用webhook”, '由于超时而调用webhook', '客户端命名空间断开连接', “服务器命名空间断开连接”, “交通关闭”, “ping超时”, “传输错误” 因此,典型的返回值是:[5,7,3,6,8,3,2]。每个单元格统计每个断开连接类型的数据库中的文档数 现
disconnectType
的文档数,并将其作为数组返回。
正如您在模型模式中看到的,这些是disconnectType
s:
“调用webhook”,
'由于超时而调用webhook',
'客户端命名空间断开连接',
“服务器命名空间断开连接”,
“交通关闭”,
“ping超时”,
“传输错误”
因此,典型的返回值是:[5,7,3,6,8,3,2]
。每个单元格统计每个断开连接类型的数据库中的文档数
现在,正如您所看到的,有一个字段device
。我想用设备
拆分此数组。现在返回值是:
{
'0': [1,2,3,4,5,6,7],
'1': [1,2,3,4,5,6,7],
}
我怎么做
这是我的代码,它只返回数字数组(不带拆分):
这在我的设备上起作用:
const ocrDisconnectTypesData = await OCRLog.aggregate([
{
$match: {
timestamp: {
$gte: moment(new Date(startDate)).tz('Asia/Jerusalem').startOf('day').toDate(),
$lte: moment(new Date(endDate)).tz('Asia/Jerusalem').endOf('day').toDate()
},
type: 'id',
eventName: 'disconnect_type',
},
},
{
$group: {
_id: '$device',
'calling webhook': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'calling webhook'] },
1,
0
]
}
},
'calling webhook because of timeout': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'calling webhook because of timeout'] },
1,
0
]
}
},
'client namespace disconnect': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'client namespace disconnect'] },
1,
0
]
}
},
'server namespace disconnect': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'server namespace disconnect'] },
1,
0
]
}
},
'transport close': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'transport close'] },
1,
0
]
}
},
'ping timeout': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'ping timeout'] },
1,
0
]
}
},
'transport error': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'transport error'] },
1,
0
]
}
},
}
},
]);
此外,查询的输出类似于:
[{
"_id" : 0,
"calling webhook" : 1.0,
"calling webhook because of timeout" : 0.0,
"client namespace disconnect" : 0.0,
"server namespace disconnect" : 0.0,
"transport close" : 0.0,
"ping timeout" : 0.0,
"transport error" : 0.0
},
{
"_id" : 1,
"calling webhook" : 1.0,
"calling webhook because of timeout" : 0.0,
"client namespace disconnect" : 0.0,
"server namespace disconnect" : 0.0,
"transport close" : 0.0,
"ping timeout" : 0.0,
"transport error" : 0.0
}]
您需要$group
根据device@AbishekKumar如果你有想法,我想让你试着回答。因为我尝试了几个小时都没有成功
const ocrDisconnectTypesData = await OCRLog.aggregate([
{
$match: {
timestamp: {
$gte: moment(new Date(startDate)).tz('Asia/Jerusalem').startOf('day').toDate(),
$lte: moment(new Date(endDate)).tz('Asia/Jerusalem').endOf('day').toDate()
},
type: 'id',
eventName: 'disconnect_type',
},
},
{
$group: {
_id: '$device',
'calling webhook': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'calling webhook'] },
1,
0
]
}
},
'calling webhook because of timeout': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'calling webhook because of timeout'] },
1,
0
]
}
},
'client namespace disconnect': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'client namespace disconnect'] },
1,
0
]
}
},
'server namespace disconnect': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'server namespace disconnect'] },
1,
0
]
}
},
'transport close': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'transport close'] },
1,
0
]
}
},
'ping timeout': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'ping timeout'] },
1,
0
]
}
},
'transport error': {
$sum: {
$cond: [
{ $eq: ['$disconnectType', 'transport error'] },
1,
0
]
}
},
}
},
]);
[{
"_id" : 0,
"calling webhook" : 1.0,
"calling webhook because of timeout" : 0.0,
"client namespace disconnect" : 0.0,
"server namespace disconnect" : 0.0,
"transport close" : 0.0,
"ping timeout" : 0.0,
"transport error" : 0.0
},
{
"_id" : 1,
"calling webhook" : 1.0,
"calling webhook because of timeout" : 0.0,
"client namespace disconnect" : 0.0,
"server namespace disconnect" : 0.0,
"transport close" : 0.0,
"ping timeout" : 0.0,
"transport error" : 0.0
}]