Unable to import .csv into mongodb using mongoimport

混江龙づ霸主 提交于 2019-12-11 20:24:21

问题


I am doing some tests to see how I can import a simple .csv file into mongodb but it mongodb keeps returning "0 objects imported. Here are my steps:

1) create a simple .csv file named "a4.csv" in mongodb/bin with 4 columns and 6 records:

Model       Make    Price   Mileage
audi        a5      500     500
mercedes    cla     333     434
ford        fusion  555     500
mazda       miata   222     434
nissan      370z    111     500
porsche     cayenne 333     434

2) Run import command:

./mongoimport -d test -c cars --type csv --file a4.csv --headerline

Output:

tins-MBP:bin tinzors$ ./mongoimport -d test -c cars --type csv --file a4.csv --headerline
connected to: 127.0.0.1
2015-02-11T12:23:11.656-0500 imported 0 objects

There's no error anything. Can someone help me? Thank you!


回答1:


I think problem is in csv file format

a4.csv file placed in mongodb bin folder

Model,Make,Price,Mileage
audi,a5,500,500
mercedes,cla,333,434
ford,fusion,555,500
mazda,miata,222,434
nissan,370z,111,500
porsche,cayenne,333,434

then

mongoimport -d test -c cars --type csv --file a4.csv --headerline

Output

D:\MongoDB\MongoDB 2.6 Standard\bin>mongoimport -d test -c cars --type csv --file a4.csv --headerline connected to: 127.0.0.1 2015-02-11T23:15:20.631+0530 imported 6 objects

> db.cars.find()
{ "_id" : ObjectId("54db93a60259aeb2c15b851d"), "Model" : "audi", "Make" : "a5", "Price" : 500, "Mileage" : 500 }
{ "_id" : ObjectId("54db93a60259aeb2c15b851e"), "Model" : "mercedes", "Make" : "cla", "Price" : 333, "Mileage" : 434 }
{ "_id" : ObjectId("54db93a60259aeb2c15b851f"), "Model" : "ford", "Make" : "fusion", "Price" : 555, "Mileage" : 500 }
{ "_id" : ObjectId("54db93a60259aeb2c15b8520"), "Model" : "mazda", "Make" : "miata", "Price" : 222, "Mileage" : 434 }
{ "_id" : ObjectId("54db93a60259aeb2c15b8521"), "Model" : "nissan", "Make" : "370z", "Price" : 111, "Mileage" : 500 }
{ "_id" : ObjectId("54db93a60259aeb2c15b8522"), "Model" : "porsche", "Make" : "cayenne", "Price" : 333, "Mileage" : 434 }

Hope it helps.




回答2:


So I THINK part of the issue is I created the file in Excel then saved it as a .csv file that that might have corrupted the file somehow. So I opened the original .csv file in a text editor (Sublime), copied the data into a brand new Sublime document and re-saved it as .csv and it worked!



来源:https://stackoverflow.com/questions/28460558/unable-to-import-csv-into-mongodb-using-mongoimport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!