简体   繁体   中英

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

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

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.

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/

一个简单的命令

mongorestore --db folder_name ./folder_name/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM