我有以下模式: const mongoose = require('mongoose');
const ocrLogSchema = new mongoose.Schema({
type: {
type: String,
required: true,
enum: ['id', 'dl'],
},
caseId: {
type: String,
required: false,
},
eventName: {
type: String,
required: true,
enum: ['connect', 'disconnect', 'disconnect_type'],
},
disconnectType: {
type: String,
required: false,
enum: [
'calling webhook',
'calling webhook because of timeout',
'client namespace disconnect',
'server namespace disconnect',
'transport close',
'ping timeout',
'transport error',
],
},
device: {
type: Number,
required: false,
enum: [0, 1],
},
timestamp: {
type: Date,
required: true,
}
}, {
timestamps: true
});
const OCRLog = mongoose.model('OCRLog', ocrLogSchema);
module.exports = OCRLog;我计算每个disconnectType的文档数,并将其作为数组返回。正如您在模型架构中所看到的,这些是disconnectType:“调用webhook”、“由于超时而调用webhook”、“客户端命名空间断开连接”、“服务器命名空间断开连接”、“传输关闭”、“ping超时”、“传输错误”。
因此,典型的返回值将是:例如[5,7,3,6,8,3,2]。每个单元格统计每个disconnectType的数据库中的文档数。
现在,正如您所看到的,有一个字段device。我想用device来拆分这个数组。所以现在返回值是:
{
'0': [1,2,3,4,5,6,7],
'1': [1,2,3,4,5,6,7],
}我该怎么做呢?
这是我的代码,它只返回数字数组(不带分隔符):
const getIDsOCRDisconnectTypesData = async (startDate, endDate) => {
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: '1',
'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
]
}
},
}
},
]);
if (ocrDisconnectTypesData.length === 0) {
return new Array(8).fill(0);
}
delete ocrDisconnectTypesData[0]._id;
return Object.values(ocrDisconnectTypesData[0]);
}发布于 2020-11-09 14:00:31
这在我的设备上有效:
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
}]https://stackoverflow.com/questions/64739153
复制相似问题