Index ¦ Archives ¦ Atom

How to Use mongodump and mongorestore Commands on Heroku

Recently I've needed to troubleshoot a production code issue on Heroku. I'm not completely familiar with ways to change code in production without doing a full git push heroku master so I just find it easier to pull production data to my local mongo instance and then troubleshoot locally. First, I sometimes have a remnant hanging around of the database and just need to clear it out. Another alternative would be to change the name of the database on import and alter the code to use the imported database instead, especially if I needed existing data for development work.

I first run a command found on StackOverflow:

$ mongo <dbname> --eval "db.dropDatabase()"

After dropping my local database, I run mongodump and connect to my heroku instance. I need the username, host, port, database, and password to connect so I use a heroku command to get that information for mongodump.

$ heroku config --app <appname>

Then running mongodump is easy. Just plug in the values. The database name is the path after the host. The command looks something like:

$ mongodump --db <path> --host <host> --port <port> --username <username> --password <password> --out <folder for dump>

Then run mongorestore to pull in the database locally:

$ mongorestore --db <dbname> <path to database dump>

The path to the database dump is the name of the folder for the dump, and then the sub-folder named after the database path. Now you have a database that looks just like production. I'm dealing with rather small datasets so this works for now. One can get more granular and just dump specific collections or even specific queries.

© Steve Spigarelli. Built using Pelican. Theme by Giulio Fidente on github.