2014-09-29 143 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