2017-10-14 60 views
0

我坚持的东西,需要你的帮助。 我想对下面的集合执行mongo聚合查询,以便我可以从boxWeight1和boxWeight2中获取每个子数组列表的最大总和权重,即sum+=max[boxWeight1,boxWeight2] for each array item in boxList array,而其他字段应该按照它的样子投影,但使用不同的键名称。在Mongo总结每个数组元素后发现最大从每个

收藏是有点像这样...

{ 
    _id: '', 
    email: '[email protected]', 
    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': '[email protected]', 
    'Order Number': '12345', 
    'Total Weight': '14.0' // 4 + 8 + 2 
} 

我希望你明白我的问题。 在此先感谢。

回答

0

可否请您尝试以下查询:

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 
          } 
      } 
    ]); 
+0

是的,我只是试过这个。我已经在上面发布了我的答案。 感谢您的回答。现在我确信我的查询在某种程度上是正确的。 –

0

好这个工作...

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列结果。之后可以用另一个投影去除它。

0

您可以使用运算符的组合来实现最新的3.4版本。

以下查询总计最大元素列表。

$objectToArray + $max将转换为array-view(k和v对的数组)并为每个元素选择max。

$map输出最大值列表,然后输出$sum以总计最大值。

喜欢的东西

{ 
    "$addFields": { 
    "Total_Weight": { 
     "$sum": { 
     "$map": { 
      "input": "$boxList", 
      "in": { 
      "$let": { 
       "vars": { 
       "weigh": { 
        "$objectToArray": "$$this" 
       } 
       }, 
       "in": { 
       "$max": "$$weight.v" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

可以简化为以下,如果你只是有两个属性。

{ 
    "$addFields": { 
    "Total_Weight": { 
     "$sum": { 
     "$map": { 
      "input": "$boxList", 
      "as": "result", 
      "in": { 
      "$max": [ 
       "$$result.boxWeight1", 
       "$$result.boxWeight2" 
      ] 
      } 
     } 
     } 
    } 
    } 
} 
相关问题