简体   繁体   中英

update and insert in mongodb import

The following command is correctly importing the data from csv file. But the problem is that there are 2 entries for the same number. I need both the entries for 417176718 in the same document (so no $set). How do I keep both these values using mongo import?

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

This is exactly what a map reduce is for.

Once you've got this in the db, run a map reduce like this:

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')

This should give you data that a single record per number with a list that contains the calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM