Jaffer Wilson
Jaffer Wilson

Reputation: 7273

How to import all files from a folder to MongoDB?

I have a folder containing CSVfiles. I want to upload the data of all the CSVs to MongoDB. I have tried the following command:

for i in *.csv; do mongoimport -d mydatabase -c ${i%.*} --type csv --file $i --headerline ; done

I have modified it to suit my scenario. The modified command is as follows:

for i in "C:\Users\lenovo-pc\Desktop\Testing sample csv\*.csv"; do mongoimport -d Better -c TESTCSV --type csv --file $i --headerline ; done

But It is giving error: i was unexpected at this time.
I would like to know how I can upload all the CSVs from a folder at once. I do not want to upload them one by one. Kindly help.

Upvotes: 0

Views: 3220

Answers (2)

ggagliano
ggagliano

Reputation: 1084

I've made this script for Windows that imports all CSVs in the same folder and names each collection with the name of the respective CSV.

You need to copy these lines in a .bat file, then edit the variables MONGO_HOME and db as you need:

TITLE MongoDB CSV Importer
SET "MONGO_HOME=C:\Program Files\MongoDB\Server\3.6"
SET db=datasets

for %%v in (*.csv) do "%MONGO_HOME%\bin\mongoimport.exe" -d %db% -c %%~nv --type CSV --file %%v --headerline
TITLE "Import completed!"
PAUSE

and this works the same for Linux shell scripts (.sh):

db="datasets"

for entry in *".csv"
do
  coll=$(echo "$entry" | cut -f 1 -d '.')
  echo $name
  mongoimport -d $db -c $coll --type CSV --file $coll".csv" --headerline
done
echo "end of import."

Hope this helps

Upvotes: 1

ares
ares

Reputation: 4413

Try this:

for %i in (*.csv) do mongoimport -d Better -c TESTCSV --type csv --file "%i" --headerline

Make sure you run this from the directory in which the *.csv files are present.

Upvotes: 2

Related Questions