2017-08-03 53 views
-3

集合存在如下:如何根據mongodb中的值獲取多個字段的計數?

[ 
    {"currentLocation": "Chennai", "baseLocation": "Bengaluru"}, 
    {"currentLocation": "Chennai", "baseLocation": "Bengaluru"}, 
    {"currentLocation": "Delhi", "baseLocation": "Bengaluru"}, 
    {"currentLocation": "Chennai", "baseLocation": "Chennai"} 
] 

預期輸出:

[ 
    {"city": "Chennai", "currentLocationCount": 3, "baseLocationCount": 1}, 
    {"city": "Bengaluru", "currentLocationCount": 0, "baseLocationCount": 3}, 
    {"city": "Delhi", "currentLocationCount": 1, "baseLocationCount": 0} 
] 

我曾嘗試是:

db.getCollection('users').aggregate([{ 
     $group: { 
      "_id": "$baselocation", 
      baseLocationCount: { 
       $sum: 1 
      } 
     }, 
    }, { 
     $project: { 
      "_id": 0, 
      "city": "$_id", 
      "baseLocationCount": 1 
     } 
}]) 

得到結果爲:

[ 
    {"city": "Chennai", "baseLocationCount": 1}, 
    {"city": "Bengaluru", "baseLocationCount": "3"} 
] 

我我不是家人我和蒙戈在一起,有什麼幫助嗎?

MongoDB的版本 - 3.4

+2

讓我們知道你在這裏嘗試 – sidgate

+0

覈對答案:https://開頭stackoverflow.com/questions/45307580/group-by-day-with-multiple-date-fields/45312816#45312816 – dnickless

回答

0

尼爾·倫恩和我有超過這個話題有一天,你可以閱讀所有關於這裏可愛的說法:Group by day with Multiple Date Fields

以下是針對您的確切問題的兩種解決方案。

第一個使用$facet階段。但請記住,它可能不適用於大型集合,因爲$facet會生成一個單個(可能很大)的文檔,該文檔可能大於當前的MongoDB文檔大小限制16MB(這僅適用於結果文檔,並且不會是流水線加工過程中出現問題反正):

collection.aggregate(
    { 
    $facet: 
    { 
     "current": 
     [ 
     { 
      $group: 
      { 
      "_id": "$currentLocation", 
      "currentLocationCount": { $sum: 1 } 
      } 
     } 
     ], 
     "base": 
     [ 
     { 
      $group: 
      { 
      "_id": "$baseLocation", 
      "baseLocationCount": { $sum: 1 } 
      } 
     } 
     ] 
    } 
    }, 
    { $project: { "result": { $setUnion: [ "$current", "$base" ] } } }, // merge results into new array 
    { $unwind: "$result" }, // unwind array into individual documents 
    { $replaceRoot: { newRoot: "$result" } }, // get rid of the additional field level 
    { $group: { "_id": "$_id", "currentLocationCount": { $sum: "$currentLocationCount" }, "baseLocationCount": { $sum: "$baseLocationCount" } } }, // group into final result) 
    { $project: { "_id": 0, "city": "$_id", "currentLocationCount": 1, "baseLocationCount": 1 } } // group into final result 
) 

第二個作品基礎上,$map階段,而不是:

collection.aggregate(
    { 
    "$project": { 
     "city": { 
     "$map": { 
      "input": [ "current", "base" ], 
      "as": "type", 
      "in": { 
      "type": "$$type", 
      "name": { 
       "$cond": { 
       "if": { "$eq": [ "$$type", "current" ] }, 
       "then": "$currentLocation", 
       "else": "$baseLocation" 
       } 
      } 
      } 
     } 
     } 
    } 
    }, 
    { "$unwind": "$city" }, 
    { 
    "$group": { 
     "_id": "$city.name", 
     "currentLocationCount": { 
     "$sum": { 
      "$cond": { 
      "if": { "$eq": [ "$city.type", "current" ] }, 
      "then": 1, 
      "else": 0 
      } 
     } 
     }, 
     "baseLocationCount": { 
     "$sum": { 
      "$cond": { 
      "if": { "$eq": [ "$city.type", "base" ] }, 
      "then": 1, 
      "else": 0 
      } 
     } 
     } 
    } 
    } 
) 
+0

謝謝尼克!完美的作品! – Sakthivel

相關問題