Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

859
Views
mongodb import multiple collections at once

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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

According to the documentation

New in version 2.6: If you do not specify --collection, mongoimport takes the collection name from the input filename. MongoDB omits the extension of the file from the collection name, if the input file has an extension.

So it seems it is supposed to do import one collection at a time. So unless you write a shell script to do it, I don't see a way.

mongodump and mongorestore are much better for taking full db dump and restore it at once as the other part of the same document says

Warning

Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.

over 4 years ago · Santiago Trujillo Report

0

It is very surprising that there isn't easy to find documentation on importing multiple collections. Restoring a database isn't always intuitive keyword when you are searching for it, but that is what you want to do when you want to restore a database from a backup. If you already have a dump of collections that you exported with mongodump, you should be able to use mongorestore.

This is all you really need to run:

mongorestore --db db_name ./db_dumpfiles/
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!