3

I have some mongoDB dump files(with .bson and .json formats) exported from my centos7 server. Is there anyway to Import them in my MongoDB on win10? I'm using mongorestore command but its cannot be found while I set an environment variable with "mongorestore" name and ".../mongorestore.exe" value and mongod.exe is running.

Gholamreza Eghbali
  • 301
  • 1
  • 4
  • 10
  • Possible duplicate of [How do I export/dump mongodb database?](https://stackoverflow.com/questions/11041614/how-do-i-export-dump-mongodb-database) – NanoPish Jul 26 '18 at 12:56
  • @NanoPish Maybe... But I get "mongorestore command not found", I set an environment variable with "mongorestore" name and "mongorestore.exe" value while mongod is running, but still the same – Gholamreza Eghbali Jul 26 '18 at 13:53
  • 1
    From another comment: This worked for me, and to add a few simple steps, I opened command prompt in the folder where mongodump.exe resides, after mongod.exe is running, then could run the mongodump -h localhost -d database_name -o C:\DestinationFolder command and it worked. – NanoPish Jul 26 '18 at 13:56
  • @NanoPish Bingo! Thanks... – Gholamreza Eghbali Jul 26 '18 at 19:25

3 Answers3

9

How to backup and restore databases

Start Mongo, open a new tab in terminal. First navigate to the folder where you want to save the backup, then type the following command.

Backup single database:

mongodump --host localhost --port 27017 --db db_name

Restore single database:

mongorestore --host localhost --port 27017 --db **** dump/db_name

(In this case, **** represents any name for the database)

Backup all databases:

mongodump --host localhost --port 27017

Restore all databases:

mongorestore --host localhost --port 27017  dump

On Windows, open command prompt in the folder where mongodump.exe resides, after mongod.exe is running, then run the mongodump command and it works.

NanoPish
  • 1,379
  • 1
  • 19
  • 35
  • 1
    For anyone looking for a link to download these mongodump.exe/mongorestore.exe files try here: https://www.mongodb.com/try/download/database-tools – Hylle Oct 27 '20 at 20:20
  • One can also use a standard MongoDB URI with the --uri option in mongodump ie. ```mongodump --uri mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin``` – Hylle Oct 27 '20 at 20:23
0

As @NanoPish mentioned in the comments, If setting "Environment Path" didn't work for mongoDBs command's (such as mongodump,mongoexport,mongoimport,mongostat,mongorestore and etc.) you can navigate to /bin directory where your mongoDB is installed and then run the commands as you wish!

Gholamreza Eghbali
  • 301
  • 1
  • 4
  • 10
  • @NanoPish Its impossible to accept a comment as an answer in stackoverflow,I voted for your comment and then mentioned you only to appreciate you copying that comment for me. This answer is only for purpose of learning and help others. – Gholamreza Eghbali Jul 31 '18 at 18:12
  • there is an answer, not only a comment! ;) – NanoPish Oct 28 '20 at 21:50
0

If the above commands don't work in windows, you can navigate to /bin directory where your MongoDB is installed and then run the mongodump.exe which will create the dump files of all the databases present. To restore a database, copy the dump folder into the /bin directory and execute mongorestore.exe

hemanth
  • 16
  • 1