MongoDB: Import/Export a Database with Command Line

Last updated on September 12, 2021 A Goodman Loading... Post a comment

If you’re using MongoDB, you can export or import a whole database by using a single command line. All of the collections of that database will be exported/imported at once. It’s super convenient when backup/restore data.

Exporting a database

Run the following command from your system command line:

mongodump -d [your database name] -o [output directory]

If you want to export a remote database with a password, run:

mongodump --host [host address] --port 27017 --db [your database name] --username [your user name] --password [your password] --out [output directory]

You can find more information about the mongodump utility in the documentation.

Importing a whole database

In order to import or restore a database, execute the following command:

mongorestore -d [your database name] [backup directory]

The larger your database, the longer it will take. You can find more details about mongorestore here.

Happy coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You May Also Like