2017-04-05 21 views
0

私はMongoDBの中にネストされた集計で国、州、郡と市の総人口を見つけるために探して、次のコレクションMongoDBの集計

{ "data" :[ 

    { "country" :"USA", "state": "NJ", "County" : "Middlesex", "city" : "Edison", "population" :0923422 }, 

    { "country" :"USA", "state": "NJ", "County" : "Mercer", "city" : "Princeton", "population" :0923422 }, 
    { "country" :"USA", "state": "NY", "County" : "Middlesex", "city" : "Edison", "population" :234234 }, 
    { "country" :"USA", "state": "NY", "County" : "Ocean", "city" : "Albony", "population" :234324 }, 
    { "country" :"USA", "state": "PA", "County" : "Central", "city" : "Edison", "population" :0923422 }, 
    { "country" :"USA", "state": "NJ", "County" : "Middlesex", "city" : "Edison", "population" :23432 }, 
    { "country" :"USA", "state": "OH", "County" : "Middlesex", "city" : "Cincinatti", "population" :0923422 }, 
    { "country" :"USA", "state": "OH", "County" : "Middlesex", "city" : "Columbos", "population" :23432 } ] 

} 

を持っています。

質問にお役立てください。

+0

本当に挑戦的なもの。異なるフォーマットでは生産できません。 – notionquest

答えて

0

問合せ: -

db.collection.aggregate([ 
{$group: 
     { 
      _id : {"country" : "$country", 
       "state" : "$state", 
       "County" : "$County", 
       "city" : "$city" 
      }, 
      "totalPopulationOfCity" : {$sum: "$population" } 

     }}, 
{$group: 
     { 
      _id : {"country" : "$_id.country", 
       "state" : "$_id.state", 
       "County" : "$_id.County"    
      },    
      "cities" : { $addToSet: {"city" : "$_id.city", "totalPopulationOfCity" : "$totalPopulationOfCity"} } , 
      "totalPopulationOfCounty" : {$sum: "$totalPopulationOfCity" } 

     }},   
{$group: 
     { 
      _id : {"country" : "$_id.country", 
       "state" : "$_id.state" 

      },    

      "counties" : {$addToSet: {"County": "$_id.County", "cities" : "$cities", 
       "totalPopulationOfCounty" : "$totalPopulationOfCounty"} } , 
      "totalPopulationOfState" : {$sum: "$totalPopulationOfCounty" }    
     }},   

{$group: 
     { 
      _id : {"country" : "$_id.country"          
      },    
      "states" : {$addToSet : {"state" : "$_id.state", "counties": "$counties", "totalPopulationOfState" : "$totalPopulationOfState"}},        
      "totalPopulationOfCountry" : {$sum: "$totalPopulationOfState" }    
     }},     

]); 

出力: - Excelを使用して

/* 1 */ 
{ 
    "_id" : { 
     "country" : "USA" 
    }, 
    "states" : [ 
     { 
      "state" : "NY", 
      "counties" : [ 
       { 
        "County" : "Ocean", 
        "cities" : [ 
         { 
          "city" : "Albony", 
          "totalPopulationOfCity" : 234324 
         } 
        ], 
        "totalPopulationOfCounty" : 234324 
       }, 
       { 
        "County" : "Middlesex", 
        "cities" : [ 
         { 
          "city" : "Edison", 
          "totalPopulationOfCity" : 234234 
         } 
        ], 
        "totalPopulationOfCounty" : 234234 
       } 
      ], 
      "totalPopulationOfState" : 468558 
     }, 
     { 
      "state" : "NJ", 
      "counties" : [ 
       { 
        "County" : "Mercer", 
        "cities" : [ 
         { 
          "city" : "Princeton", 
          "totalPopulationOfCity" : 923422 
         } 
        ], 
        "totalPopulationOfCounty" : 923422 
       }, 
       { 
        "County" : "Middlesex", 
        "cities" : [ 
         { 
          "city" : "Edison", 
          "totalPopulationOfCity" : 946854 
         } 
        ], 
        "totalPopulationOfCounty" : 946854 
       } 
      ], 
      "totalPopulationOfState" : 1870276 
     }, 
     { 
      "state" : "OH", 
      "counties" : [ 
       { 
        "County" : "Middlesex", 
        "cities" : [ 
         { 
          "city" : "Cincinatti", 
          "totalPopulationOfCity" : 923422 
         }, 
         { 
          "city" : "Columbos", 
          "totalPopulationOfCity" : 23432 
         } 
        ], 
        "totalPopulationOfCounty" : 946854 
       } 
      ], 
      "totalPopulationOfState" : 946854 
     }, 
     { 
      "state" : "PA", 
      "counties" : [ 
       { 
        "County" : "Central", 
        "cities" : [ 
         { 
          "city" : "Edison", 
          "totalPopulationOfCity" : 923422 
         } 
        ], 
        "totalPopulationOfCounty" : 923422 
       } 
      ], 
      "totalPopulationOfState" : 923422 
     } 
    ], 
    "totalPopulationOfCountry" : 4209110 
} 

検証: -

enter image description here

+0

ありがとうございます。それは多くを助ける。 –

+0

それが役に立ったら答えを受け入れることができますか?ありがとう! – notionquest