0

我正在做一些测试,看看如何将一个简单的 .csv 文件导入 mongodb,但 mongodb 不断返回“0 个导入的对象。这是我的步骤:

1)在 mongodb/bin 中创建一个名为“a4.csv”的简单 .csv 文件,其中包含 4 列和 6 条记录:

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)运行导入命令:

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

输出:

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

没有任何错误。有人能帮我吗?谢谢!

4

2 回答 2

3

我认为问题出在csv文件格式上

a4.csv文件放在 mongodb bin 文件夹中

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

然后

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

输出

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 }

希望能帮助到你。

于 2015-02-11T18:26:23.367 回答
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!

于 2015-02-11T20:07:30.333 回答