【问题标题】:In Mongo Sum for each array elements after finding Max from each在从每个元素中找到 Max 后,在 Mongo Sum 中为每个数组元素
【发布时间】:2017-10-14 10:46:08
【问题描述】:

我遇到了一些问题,需要您的帮助。 我想对下面的集合执行 mongo 聚合查询,以便我可以从 boxWeight1 和 boxWeight2 中获取每个子数组列表的总权重作为最大值的总和,即sum+=max[boxWeight1,boxWeight2] for each array item in boxList array 和其他字段应该按原样投影但具有不同键名。

收藏有点像这样……

{
     _id: '',
     email: 'yugal121@gmail.com',
     number: 12345,
     boxDetail: {
        boxname: 'package_yugal',
        boxList: [
            {
               boxWeight1: '4.0',    //this is max here. so it will be added
               boxWeight2: '2.0'
            }.
            {
               boxWeight1: '4.0',
               boxWeight2: '8.0'     //this is max here. so it will be added
            } 
            {
               boxWeight1: '0.0',
               boxWeight2: '2.0'     //this is max here. so it will be added
            }
        ]
    } 
}

所以上述集合在执行查询后的结果应该是这样的:

{
    'User Mail': 'yugal121@gmail.com',
    'Order Number': '12345',
    'Total Weight': '14.0'   // 4 + 8 + 2 
}

我希望你能理解我的问题。 提前致谢。

【问题讨论】:

    标签: arrays mongodb mongodb-query projection


    【解决方案1】:

    你能试试下面的查询吗:

    db.collection.aggregate([
               {
                  $unwind : "$boxList"
               },
               {
                  $project : {
                               "email" : 1,
                               "number" : 1,
                               "maxWeight" : { $cond : { if : { $gt : 
                                   ["$boxList.boxWeight1","$boxList.boxWeight2"]} , 
                                                        then : "$boxList.boxWeight1" , 
                                                        else : "$boxList.boxWeight2"}
                                             }
                             }
                },
                {
                    $group : { 
                                _id : "$_id" , 
                                Total : {  $sum : "$maxWeight" }, 
                                email : {  $first : "$email" },
                                number : {$first : "$number"}
                            }
               },
               {
                   $project : { 
                                "User Mail" : "$email" , 
                                "Order Number" : "$number",
                                "Total Weight" : "$Total",
                                _id : 0
                              }
                }
      ]);
    

    【讨论】:

    • 是的,我只是有点像这样尝试过。我已经在上面发布了我的答案。谢谢你的回答。现在我确信我的查询在一定程度上是正确的。
    【解决方案2】:

    这行得通...

    db.collection.aggregate([
    {   
        $project: {
             email: '$email',
             number: '$number',
             boxList: '$boxDetail.boxList'
        },
        {    $unwind: '$boxList'   },
        {
             $group: {
                 _id: '$_id',
                 'Total Weight': {
                    $sum:{
                            $max:['$boxdetail.BoxActualWeight', '$boxdetail.BoxDimWeight']
                        }
                  },
                  'User Email': {
                    $first: '$email'
                  },
                  'Order Number':{
                    $first: '$number'
                  }
             }
    
        }
    }])
    

    这会给出带有 _id 列的结果。可以在其后使用另一个投影将其移除。

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 2020-06-06
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多