2017-10-14 121 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" 
      ] 
      } 
     } 
     } 
    } 
    } 
}