2017-05-01 13 views
1

私はMongoDBのにインポートcsvファイルを持って、それは以下の構造を有する:IDとしてスタート、および他のすべてのフィールドと私はグループにこれをしたいグループ/集計フィールド

enter image description here

をそのようなのようなもののチャイルズ、よう:

"_id" : "A" { 
      "destination" : B { 
       "duration" : 5 
       "duration" : 3 
       "duration" : 6 
      } 
      "destination" : C { 
       "duration" : 10 
      } 
    } 
    "_id" : "B" { 
      "destination" : A { 
       "duration" : 4 
      } 
    } 

私はMongoDBのの集約コマンドの基本を理解し、私はヨーゲッシュの答えに基づいている、これを試してみました:

db.test.aggregate([ 
{ "$group": { 
    "_id": "$start", 
    "Trips": { 
     "$push": { 
      "Destination" : "$destination", 
      "Duration"  : "$duration" 
     } 
    } 
    } 
}],{allowDiskUse:true}).pretty() 

私の質問は、期間をグループ化する方法です。

{ "Destination": B, 
     "Duration": 5 
    }, 
    { 
     "EndStationID": B, 
     "Duration": 3 
    } 

を私もこのような構造にそれらを組み合わせることができますどのように:あなたのCSVを1として

{ "Destination": B { 
      "Duration": 5, 
      "Duration": 3 
    } 

答えて

1

今、AからBへの2「旅行」のは、そのようなのような2つのエントリがあります構造私はあなたがgroup開始し、先のフィールドをして、以下のように集約クエリので、配列に期間をプッシュする必要があり、次のコレクション

{ "start" : "A", "destination" : "B", "duration" : 5 } 
{ "start" : "A", "destination" : "B", "duration" : 3 } 
{ "start" : "A", "destination" : "B", "duration" : 6 } 
{ "start" : "B", "destination" : "A", "duration" : 4 } 

ファーストを作成しました:

db.collectionName.aggregate({ 
    "$group": { 
    "_id": { 
     "start": "$start", 
     "dest": "$destination" 
    }, 
    "duration": { 
     "$push": { 
      "duration": "$duration" 
     } 
    } 
    } 
}, { 
    "$project": { 
    "_id": "$_id.start", 
    "destination": "$_id.dest", 
    "duration": 1 
    } 
}).pretty() 
+1

ありがとうございました。ありがとうございました。私は、ここでの回答と組み合わせて部分的にあなたのアプローチを使用しました。http://stackoverflow.com/questions/16772156/nested-grouping-with-mongodb – ffritz

関連する問題