How to load a CSV file to MongoDB Atlas?

Hello! I want to load a CSV file to MongoDB Atlas.

How to do this?

I would use the command line utility mongimport (connection example. available in the Atlas UI for each under the Command Line Tools tab under the cluster). More detail here

1 Like

I use this import string from Command Line Tools bar:

mongoimport --uri mongodb+srv://tudor_15:<PASSWORD><DATABASE> --collection <COLLECTION> --type <FILETYPE> --file <FILENAME> 

Here I put my password, database name, collection name, CSV file type and my file name.

I run the string in command line, and it throws me the next error:

The syntax of the command is incorrect.

How can I get rid of this error and import the CSV?

Can you paste in your exact command line you are trying to use (except the password)?

1 Like

Sure, this is the line I’m trying to use:

mongoimport --uri mongodb+srv://tudor_15:<PASSWORD><Database1> --collection <Invoice_Train> --type <CSV> --file <invoice_train.csv>

remove the < > brackets

something like:

mongoimport --uri mongodb+srv:// --collection Invoice_Train --type CSV --file invoice_train.csv

I ran this line of code, without the < > brackets, but I got an error again:

The system cannot find the file specified.

This is strange, because in command line I am in the file’s folder. I also tried to use the absolute path, but nothing changed.

How could I get rid of this error?

What is your OS?
If it is Windows check how the file was saved
It may be having different extension

It’s Windows 10. The file is saved “invoice_train.csv”.

These are the files I use:

Please show the exact file you are trying to load from your system by dir command and show few lines from your file

The sample .csv files from your link appear to be in excel format

csv is comma separated

I tried to load a sample .csv file with your command on my Windows machine.It worked fine

Here is the output of the “dir” command, run on the folder with the files I’m working with:

05/11/2021  01:03 PM    <DIR>          .
05/11/2021  01:03 PM    <DIR>          ..
08/24/2020  12:30 PM         2,253,632 client_test.csv
08/24/2020  12:30 PM         5,986,133 client_train.csv
05/30/2021  12:35 PM       146,577,388 invoice_test.csv
05/24/2021  08:29 PM       344,346,841 invoice_train.csv
05/11/2021  01:04 PM     1,105,521,210 invoice_train.json
08/24/2020  12:30 PM         2,153,008 SampleSubmission (2).csv
               6 File(s)  1,606,838,212 bytes
               2 Dir(s)  224,879,640,576 bytes free

A few lines from “invoice.csv” file:


Yes, my computer also detected this file as suitable for excel format, but since it’s too big for the Excel, I open it with Notepad. Otherwise, the Excel will keep 1 million rows and delete the rest (the file has 4 million rows).

Are you sure you are in the correct directory?

I downloaded client_test dump and successfully loaded into my cluster

The original file is a zipped file.When you unzip/extract the file it creates a directory client_test.csv
Under this you will see the file client_test.csv

Yes, absolutely.

Can you show me the command line which you use?

You can try yourselves.Create a simple csv file using those 10-15 records from your file and try mongoimport again.It will work

Issue could be your path or file itself

This is what i used

mongoimport --uri mongodb+srv:// --collection clienttest --type CSV --file client_test.csv --headerline

2021-06-05T19:32:36.025+0530 [########################] client.clienttest 2.15MB/2.15MB (100.0%)
2021-06-05T19:32:36.026+0530 imported 58069 documents

I ran this command with my identification data and got another error:

2021-06-06T21:51:05.980+0300 error connecting to host: could not connect to server: server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr:, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection([-190]) incomplete read of message header: EOF }, { Addr:, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection([-189]) incomplete read of message header: EOF }, { Addr:, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection([-188]) incomplete read of message header: EOF }, ] }

More precisely, this is the command line I ran:

mongoimport --uri mongodb+srv:// --collection Invoice_Train --type CSV --file invoice_train.csv --headerline

Were you able to connect to your cluster before?
What is the status of your cluster in Atlas.Any error/alerts?
Have you whitelisted your IP?
Could be temporary N/W issue or SSL issue
Share your id/pwd for us to check or create another user and share the creds

Actually, I haven’t tried yet. I intended to upload the date to MongoDB Atlas, and then to connect from Google Colaboratory.

I realized recently that I ran this command when I was in another wifi network, and when my IP adress was different. The fact that I didn’t change the IP adress in my cluster could have generated this error.

But, when I put my current IP adress and ran the command for uploading the data and the command for checking the cluster status, I got the error:

2021-06-07T12:16:27.432+0300 error connecting to host: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AtlasError) bad auth : Authentication failed.

And when I ran these commands with IP adress, I got the same error.

My id: tudor_15
My password: Centurion15

Bad auth means wrong combination of userid & pwd
The userid/pwd we are passing in the connect string are database user
Did you create the user tudor_15 in the database?

You should first check your connectivity to cluster then do the load
How did you get your connect string?
From Atlas or framed it based on another sample string?

Please login to your Atlas and check status of your cluster
Is it up and running?Is the clusterid given correct?

Please check again your steps

In the ‘Cluster’ tab, there is ‘Command Line Tools’ subtab. There are the prototypes for the main commands. You have to take the command, paste in command prompt, and replace the PASSWORD word with your own password, and in case you have to import data, you replace the other parameters in the string with yours.

The user id ‘tudor_15’ was already put in the command line strings in the ‘Command Line Tools’.

How to check the status of the cluster if the command line gives me an error?

I understood yoru problem.From shell you cannot check
Asking you to check cluster status from Atlas
When you login to your Atlas account you can see your cluster status.There should be an alerts tab/button
After you setup your Cluster you would have created a user
What is your cluster type Free Tier or paid and what steps you followed
For mongo University students there are steps on how to setup Sandbox cluster and load data

I repeat unless you have a database user you cannot connect to your cluster/db from shell nor you can perform other tasks like mongoimport etc