Search code examples
mongodbmongoimportmongoexport

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


Solution

  • 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.