【问题标题】:mongoDB group by date and other columnmongoDB 按日期和其他列分组
【发布时间】:2022-01-24 21:23:21
【问题描述】:

目前我需要一些按日期和其他列分组的帮助:

[
    {
        '$project': {
            'date': 1, 
            'source': 1, 
            'callDirection': 1, 
            'status': 1
        }
    }, {
        '$match': {
            '$or': [
                {
                    'source': '501'
                }, {
                    'source': '555'
                }
            ]
        }
    }, {
        '$group': {
            '_id': 0, 
            'total': {
                '$sum': 1
            }, 
            'answered': {
                '$sum': {
                    '$cond': [
                        {
                            '$eq': [
                                '$status', 'ANSWERED'
                            ]
                        }, 1, 0
                    ]
                }
            }, 
            'no answer': {
                '$sum': {
                    '$cond': [
                        {
                            '$eq': [
                                '$status', 'NO ANSWER'
                            ]
                        }, 1, 0
                    ]
                }
            }
        }
    }
]

我现在得到的结果是总数:

_id:0
total:591
answered:443
no answer:129

我需要的是按来源和日期拆分数据,这样我就可以得到这样的数据返回

date => 2022-01-23 , source => 501, answered => 12, noanswer => 2
date => 2022-01-23 , source => 555, answered => 5, noanswer => 5
date => 2022-01-24 , source => 501, answered => 6, noanswer => 3 
date => 2022-01-24 , source => 555, answered => 22, noanswer => 6 

示例数据:

 "date": "2021-12-23 10:25:59","source": "501","callDirection": "Outgoing","status": "ANSWERED"
  "date": "2021-12-23 11:21:19","source": "501","callDirection": "Outgoing","status": "NO ANSWER"
  "date": "2021-12-24 01:21:19","source": "501","callDirection": "Outgoing","status": "ANSWERED"
  "date": "2021-12-24 10:25:59","source": "555","callDirection": "Outgoing","status": "ANSWERED"
  "date": "2021-12-25 12:55:19","source": "555","callDirection": "Outgoing","status": "ANSWERED"

我是 mongoDb 的新手,我需要一些帮助,非常感谢

【问题讨论】:

    标签: mongodb date grouping


    【解决方案1】:

    也许是这样的:

     db.collection.aggregate([
     {
    $addFields: {
      date: {
        $substr: [
          "$date",
          0,
          10
        ]
      }
    }
    },
    {
    $group: {
      _id: {
        da: "$date",
        so: "$source",
        cd: "$callDirection"
        },
       answer: {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "ANSWERED",
                "$status"
              ]
            },
            1,
            0
          ]
         }
        },
        noanswer: {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "ANSWERED",
                "$status"
              ]
            },
            0,
            1
          ]
         }
        }
       }
     },
     {
      $project: {
      date: "$_id.da",
      source: "$_id.so",
      callDirection: "$_id.cd",
      answer: 1,
      noanswer: 1
      }
     }
     ])
    

    解释:

    1. 将日期时间字符串替换为仅日期字符串
    2. 按日期、来源和呼叫方向分组,从状态字段生成两个新的计数字段 answer 和 noanswer。
    3. 项目只需要必要的字段

    playground

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 2016-07-01
    • 2016-03-23
    • 1970-01-01
    • 2019-01-19
    • 2019-06-06
    • 2011-07-07
    • 2015-01-03
    相关资源
    最近更新 更多