首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据从聚合拆分到2个数组

将数据从聚合拆分到2个数组
EN

Stack Overflow用户
提问于 2020-11-08 22:21:06
回答 1查看 29关注 0票数 0

我有以下模式: const mongoose = require('mongoose');

代码语言:javascript
复制
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来拆分这个数组。所以现在返回值是:

代码语言:javascript
复制
{
    '0': [1,2,3,4,5,6,7],
    '1': [1,2,3,4,5,6,7],
}

我该怎么做呢?

这是我的代码,它只返回数字数组(不带分隔符):

代码语言:javascript
复制
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]);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-09 14:00:31

这在我的设备上有效:

代码语言:javascript
复制
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
                    ]
                }
            },
        }
    },
]);

此外,查询的输出类似于:

代码语言:javascript
复制
[{
  "_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
}]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64739153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档