Sample Header Ad - 728x90

How can I load csv data into mongodb and choose the types I'd like to have created for each column?

6 votes
1 answer
14934 views
I'm looking for a method to load csv data in mongodb and specify the types that I'd like to have created for each column? I've been using mongoimport but I find it quite painful because there doesn't appear to be any way to specify the datatype to be used for a particular column in the csv file. I load the data ... mongoimport --drop -d myDatabase -c myCollection --type csv --fields name --file myFile.csv Then I do analysis to see what types mongo actually created. > db.artist.itunes.feed.count() 36545 > db.artist.itunes.feed.count( { 'name' : { $type : 2 } } ) 36511 > db.artist.itunes.feed.count( { 'name' : { $type : 1 } } ) 1 > db.artist.itunes.feed.count( { 'name' : { $type : 16 } } ) 33 Then I do surgery to correct errors like so ... db.artist.itunes.feed.find( { 'name' : { $type : 1 } } ).forEach( function (x) { x.name = new String(x.name); // convert field to string db.artist.itunes.feed.save(x); }); This is tedious. It would be great if there were a method that allowed me to specify at import time what type to create for each column in the csv file like so: mongoimport --drop -d myDatabase -c myCollection --type csv --fields field1,field2 --types 2,2 --file myFile.csv http://docs.mongodb.org/manual/reference/operator/query/type/
Asked by Alex Ryan (161 rep)
Sep 20, 2014, 02:35 AM
Last activity: Nov 27, 2018, 09:06 PM