Barcelona Code School

Barcelona Code School’s blog

Follow publication

Cloning / restoring / migrating MongoDB with mongodump

Barcelona Code School
Barcelona Code School
2 min readJul 14, 2022

There is an easy way to migrate MongoDB database between different hosts. Sometimes we might need it for example in case of:

  • moving db from local database to the remote
  • creating a backup copy of the db “freezing” all the content we have to be able to restore at some time
  • getting the production db and moving to local server for some testing, development, etc..

And any other possible case you might think about.

With mongodump it’s only 2 simple steps.

First you will need to install mongodump for your platform: https://www.mongodb.com/docs/database-tools/mongodump/

Then first step is to create the dump:

mongodump --uri='URLOFMONGO'

'URLOFMONGO' is URL you are using to connect your app to MongoDB -- either local like let's say mongodb://127.0.0.1/ourDatabase or remote like mongodb+srv://yourSuperApp:superSecretPassword@cluster0.wxtxjy6.mongodb.net/superAppDB

It will save the dump — the whole content of your database — in the dumpfolder in the current folder from there the command was executed.

Inside dump folder you would see the folder with the name of the DB - it's the one you would like to use to restore.

To restore:

mongorestore --uri='URL_OF_MONGODB' -d NAME_OF_DB_TO_PUT_DATA_INTO FOLDER_WITH_DUMP

such as

mongorestore --uri='mongodb://127.0.0.1' -d winemind dump/Winemind

Now we moved data from remote DB called Winemind to localhost DB named winemind

The end :)

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Barcelona Code School
Barcelona Code School

Written by Barcelona Code School

We help people to learn web development and improve their lives by becoming a freelancer, finding a job or starting their own product. Just coding, no bullshit!

No responses yet

Write a response