2014-09-29 144 views
0

以下命令正確導入csv文件中的數據。但問題是,有相同數量的2個條目。 我需要在同一個文檔中的417176718的條目(所以沒有$集)。如何使用mongo導入保持這兩個值?更新並插入mongodb導入

cat final.txt 
number, date, duration, type, destination 
417176718 , 2013-01-23 20:09:00 , 1 , NORMAL_CLEARING , 61998487 
409334392 , 2013-01-24 11:25:18 , 40 , NO_ANSWER , 09821973636 
919480909 , 2013-01-25 20:58:00 , 40 , NORMAL_CLEARING , 09919480909 
417176718 , 2013-01-24 20:09:00 , 1 , FAILED , 61998487 

mongoimport -d mydb -c vcalls --type csv --file final.txt --headerline 
+1

我想你將不得不編寫自己的腳本來做到這一點。 MongoImport不會爲你做。 – 2014-09-29 10:31:46

回答

1

這正是地圖縮小的目的。

一旦你得到了這個在分貝,運行圖減少這樣的:

mapper= function(){emit(this.number, {'data':[{'date':this.date, 'duration':this.duration, 'type':this.type, 'destination':this.destination}]});} 

reducer = function(k,v){ 
    data=[]; 
    for (i=0;i<v.length;i++){ 
      for (j=0;j<v[i].data.length;j++){ 
       data.push(v[i].data[j]); 
     } 
    } 
    return {'data':data} 
} 
db.vcalls.mapReduce(mapper, reducer, 'reducedcalls') 

這應該給你的數據每數一個記錄與包含呼叫列表。

+0

太好了。由於某些版本問題,我不得不使用:{inline:1}。如果我有數百萬這樣的記錄,它會起作用嗎? – shantanuo 2014-09-29 13:55:00

+1

如果你想要數百萬,你會希望他們在一個新的集合。嘗試{out:'newcollection'} – 2014-09-29 17:07:27

+0

我收到一個異常:從JavaScript轉換爲BSON失敗:對象大小17037962超出16793600字節的限制#我如何傳遞此異常,因爲我不需要這麼大的文檔,但需要其他所有文檔。 – shantanuo 2014-10-01 07:38:30