Mongodb import multiple collections at once

mongodbmongoexportmongoimport

I am using this command

mongoimport –db databasename

to import a database that I exported using mongoexport.
The database has over 100 collections in the mongoimport documentation you need to specify the collection name and json file.
How do I go about importing all the collections at once without having to type a command for each collection

Best Answer

If dump files are in .json you can use the script for import

ls *.json | sed 's/.metadata.json//' | while read col; do mongoimport -d db_name -c $col < $col.metadata.json; done

If dump files are in .json.gz you can use the script for import

ls *.gz | sed 's/.metadata.json.gz//' | while read col; do mongoimport -d db_name --gzip -c $col < $col.metadata.json.gz; done