I want to load data/restore dump data in mongoDB using mongorestore. I am trying to command
mongorestore dumpbut it giving me error
Sat Sep 21 16:12:33.403 JavaScript execution failed: SyntaxError: Unexpected identifierHow can we restore or put data into mongoDB?? Please give me the steps.
914 Answers
mongodump:To dump all the records:
mongodump --db databasenameTo limit the amount of data included in the database dump, you can specify --db and --collection as options to mongodump. For example:
mongodump --collection myCollection --db testThis operation creates a dump of the collection named myCollection from the database 'test' in a dump/ subdirectory of the current working directory. NOTE: mongodump overwrites output files if they exist in the backup data folder.
mongorestore:To restore all data to the original database:
1) mongorestore --verbose \path\dumpor restore to a new database:
2) mongorestore --db databasename --verbose \path\dump\<dumpfolder>Note: Both requires mongod instances.
3Dump DB by mongodump
mongodump --host <database-host> -d <database-name> --port <database-port> --out directoryRestore DB by mongorestore
With Index Restore
mongorestore --host <database-host> -d <database-name> --port <database-port> foldernameWithout Index Restore
mongorestore --noIndexRestore --host <database-host> -d <database-name> --port <database-port> foldernameImport Single Collection from CSV [1st Column will be treat as Col/Key Name]
mongoimport --db <database-name> --port <database-port> --collection <collection-name> --type csv --headerline --file /path/to/myfile.csvImport Single Collection from JSON
mongoimport --db <database-name> --port <database-port> --collection <collection-name> --file input.json To restore a single database:
Backup the
testdbdatabase$ mongodump --db testdbRestore the
testdbdatabase to a new database calledtestdb2$ mongorestore --db testdb2 dump/testdb
To restore all databases:
Backup all databases
$ mongodumpRestore all databases
$ mongorestore dump
The directory should be named 'dump' and this directory should have a directory which contains the .bson and .json files. This directory should be named as your db name.
eg: if your db name is institution then the second directory name should be institution.
After this step, go the directory enclosing the dump folder in the terminal, and run the command
mongorestore --drop.
Do see to it that mongo is up and running.
This should work fine.
1Follow this path.
C:\Program Files\MongoDB\Server\4.2\binRun the cmd in bin folder and paste the below command
mongorestore --db <name-your-database-want-to-restore-as> <path-of-dumped-database>For Example:
mongorestore --db testDb D:\Documents\Dump\myDb - Start mongod
- Navigate to folder where you have extracted "enron.zip" in OS shell(cmd in case of windows)
- Then type ">mongorestore -d your_db_name dump/enron"
You can take a dump to your local machine using this command:
mongodump -h <host>:<port> -u <username> -p <password> -d ubertower-new -o /path/to/destination/directoryYou can restore from the local machine to your Mongo DB using this command
mongorestore -h <host>:<port> -u <username> -p <password> -d <DBNAME> /path/to/destination/directory/<DBNAME> For mongoDB database restore use this command here
mongorestore --db databasename --drop dump file path 1 You can also restore your downloaded Atlas Backup .wt WiredTiger files (which unzips or untar as a restore folder) to your local MongoDB.
First, make a backup of your /data/db path. Call it /data_20200407/db. Second, copy paste all the .wt files from your Atlas Backup restore folder into your local /data/db path. Restart your Ubuntu or MongoDB server. Start your Mongo shell and you should have those restored files there.
If you are okay to drop the existing collections then then following command works fine for me.
- Okay to drop the existing collections
$ uri_complete="your_complete_uri"
$ restoreFileName="your_restore_filename"
$ mongorestore --uri=$uri_complete -v --gzip --archive=$restoreFileName --drop- Not okay to drop existing collections
$ uri_complete="your_complete_uri"
$ restoreFileName="your_restore_filename"
$ mongorestore --uri=$uri_complete -v --gzip --archive=$restoreFileNameIf your database is running on localhost on default port without authentication then the following will work.
$ mongorestore -v --gzip --archive=restorefile.gzip --dropMore details - mongorestore
I have been through a lot of trouble so I came up with my own solution, I created this script, just set the path inside script and db name and run it, it will do the trick
#!/bin/bash
FILES= #absolute or relative path to dump directory
DB=`db` #db name
for file in $FILES
do name=$(basename $file) collection="${name%.*}" echo `mongoimport --db "$DB" --file "$name" --collection "$collection"`
done mongodump --host test.mongodb.net --port 27017 --db --username --password --authenticationDatabase admin --ssl --out
mongorestore --db --verbose
Process of Backup MongoDb Collections.
- Find the MongoDb folder in ProgramFiles or ProgramFiles32.
- Navigate to bin Folder.Find the path like this.C:\Program Files\MongoDB\Server\4.2\bin
- Make you have to Install Mongo Dump Tools.
- If you are not find any such tools , kindly go through this link and install it. MongDb Database Tools
- inside bin Folder open CMD or navigate to MongdDb Server Bin folder.
- Run this Command mongodump --db yourdatabasename. after running this command Dump creation process will start and it will create and one Dump folder in side bin.
Restore Process
open the CMD prompt in Mongdb Server bin Folder.
after that Run this Command **mongorestore --db mydababse --verbose
D:\MongoDbBackup\mydababse\Dec28-21**(this this path where I have store my backup collection).
some time mongDb Restore program required folder permission to restore.
For mongoDB database restore use this command here . First go to your mongodb database location such as For Example :cd Downloads/blank_db/v34000After that Enter mongorestore -d v34000 ./