0

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
  • Fyi you can add mongo binaries to $PATH, export PATH=<mongodb-install-directory>/bin:$PATH, then you can run mongoimport command from anywhere. Commented Feb 11, 2015 at 18:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response - could you explain to me what the difference is between your method and my method? Or should I fix the .csv file somehow? Thank you.
Hi , fix the csv file and check once. Thank you
Is it possible to import all the records from the csv into just 1 object instead of 1 object per record?
when I tried to do the same thing mongoimport -d test -c cars --type csv --file cars.csv It's giving me an error saying : [thread1] SyntaxError: missing ; before statement @(shell):1:15
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.