Import multiple CSV files to MongoDB Collection

Hello,

I have more than 2k CSV files, I want to import them into a collection
the CSV files structure is the same (column names and number of column)
I am using MongoDB in My Windows 10 PC

Thanks

Hi @Osama_Al-Tahish ,
You can write a script with a loop over the files and run mongoimport in each iteration.
Thanks,
Rafael,

1 Like

I tried many scripts but without results
can you help me to do that currectlly

My System: Windows 10 & MongoDB 4.4.4

Thanks @Rafael_Green

Hi,
First install the MongoDB database tools from here.
Then add “C:\Program Files\MongoDB\Tools\100\bin” to your Path environment variable like this:

  1. image

image
3.
image
4.
image
5.
image
6.
image
7. click save

Open Windows PowerShell and run these commands:

cd myCsvFilesDirectory
$files = Get-ChildItem .
foreach ($f in $files) {
  if ($f -Like "*.csv") {
    mongoimport -d mydb -c mycollection --type csv --file $f --headerline
  }
}

Goodluck,
Rafael,

2 Likes

Hi @Osama_Al-Tahish

in case you run into problems please add the error message and what you tried… @Rafael_Green’s script should do the job. Please note that you may get in to trouble when you have an unique index on the target collection.

Regards,
Michael

1 Like

Thanks, It’s Working But I Have a Problem With some of my CSV Files I will solve it and get back to you

The Script Work Perfectly

I got this error with some files:
Failed: read error on entry #2578: line 2579, column 40: extraneous " in field

I think its about escape internal double-quote