【问题标题】:How to calculate $avg of array items in MongoDB aggregation?如何计算 MongoDB 聚合中数组项的 $average?
【发布时间】:2019-12-12 04:48:17
【问题描述】:

我正在构建一个 mongodb aggregation 查询,其中我有一个数值的计算字段(数组)。我必须找到整个数组字段的平均值。我的mongodb聚合查询是这样的

[{$match: {
  nsp: '/globalintltd.com',
  agentEmail: 'ahmed1549@globalintltd.com'
}}, {$lookup: {
  from: 'messages',
  let : {cid : '$_id'},
  pipeline:[
    {
      $match : {
        $expr:{
          $eq: ['$cid', '$$cid'],
        },
        type: 'Agents'
      }
    },
    {$limit : 1}
  ],
  as: 'messages'
}}, {$project: {
  agentEmail: 1,
  convDate: {
    $dateFromString:{
      dateString: '$createdOn'
    }
  },
  firstReplyDate:{
    $dateFromString:{
      dateString:{
        $arrayElemAt:['$messages.date', 0]
      }
    }
  }
}}, {$addFields: {
  difference: {
    $divide:[
      {
         $subtract:[
    '$firstReplyDate',
    '$convDate'
    ] 
      },
      60000
    ]
  }
}}, {$group: {
  _id: {
    "agentEmail" : "$agentEmail"
  },
  AgentFirstResponses : {
    $push : "$difference"  
  }
}}]

从上面的查询我得到这样的结果

[
  {agentEmail : "ahmed1549@globalintltd.com", convId : 3434345111, AgentFirstResponses : [33.54,65.65,9.087,5.43]},
  {agentEmail : "ahmed1549@globalintltd.com", convId : 3434345112, AgentFirstResponses : [3.54,65.165,19.087,1.43]},
  {agentEmail : "ahmed1549@globalintltd.com", convId : 3434345113, AgentFirstResponses : [63.54,654.65,19.087,5.43]},
  {agentEmail : "ahmed1549@globalintltd.com", convId : 3434345114, AgentFirstResponses : [33.54,65.65,9.087,5.43]}
]

我想在我的aggregation 查询中为该字段应用$avg。如何使用mongodb 获得整个数组的平均值?

我想要这样的结果

{
  "agentEmail" : "ahmed1549@globalintltd.com",
  "AvgFirstResponse" : 23.665(what ever average will of the whole array)
}

【问题讨论】:

    标签: mongodb aggregation-framework aggregation


    【解决方案1】:

    将 $push 替换为 $avg,如下所示:

    [{$match: {
      nsp: '/globalintltd.com',
      agentEmail: 'ahmed1549@globalintltd.com'
    }}, {$lookup: {
      from: 'messages',
      let : {cid : '$_id'},
      pipeline:[
        {
          $match : {
            $expr:{
              $eq: ['$cid', '$$cid'],
            },
            type: 'Agents'
          }
        },
        {$limit : 1}
      ],
      as: 'messages'
    }}, {$project: {
      agentEmail: 1,
      convDate: {
        $dateFromString:{
          dateString: '$createdOn'
        }
      },
      firstReplyDate:{
        $dateFromString:{
          dateString:{
            $arrayElemAt:['$messages.date', 0]
          }
        }
      }
    }}, {$addFields: {
      difference: {
        $divide:[
          {
             $subtract:[
        '$firstReplyDate',
        '$convDate'
        ] 
          },
          60000
        ]
      }
    }}, {$group: {
      _id: {
        "agentEmail" : "$agentEmail"
      },
      AgentFirstResponses : {
        $avg : "$difference"  
      }
    }}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2020-03-19
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多