Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restore the dump into your running mongodb

I want to load data/restore dump data in mongoDB using mongorestore. I am trying to command

mongorestore dump 

but it giving me error

Sat Sep 21 16:12:33.403 JavaScript execution failed: SyntaxError: Unexpected identifier 

How can we restore or put data into mongoDB?? Please give me the steps.

like image 461
Sandeep Singh Avatar asked Sep 21 '13 10:09

Sandeep Singh


People also ask

How do I restore my MongoDB database?

The mongorestore utility restores a binary backup created by mongodump . By default, mongorestore looks for a database backup in the dump/ directory. The mongorestore utility restores data by connecting to a running mongod directly. mongorestore can restore either an entire database backup or a subset of the backup.

Where is MongoDB dump folder?

By default, mongorestore looks for a database backup in mongodb's bin\dump folder which is also the default folder for mongodump command for dumping the backup. Example: In this example, we are using a database GeeksForGeeks which has 4 collections.


2 Answers

mongodump: To dump all the records:

mongodump --db databasename 

To 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 test 

This 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\dump 

or restore to a new database:

2) mongorestore --db databasename --verbose \path\dump\<dumpfolder> 

Note: Both requires mongod instances.

like image 156
Arman Ortega Avatar answered Oct 18 '22 20:10

Arman Ortega


Dump DB by mongodump

mongodump --host <database-host> -d <database-name> --port <database-port> --out directory 

Restore DB by mongorestore

With Index Restore

mongorestore --host <database-host> -d <database-name> --port <database-port> foldername 

Without Index Restore

mongorestore --noIndexRestore --host <database-host> -d <database-name> --port <database-port> foldername 

Import 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.csv 

Import Single Collection from JSON

mongoimport --db <database-name> --port <database-port> --collection <collection-name> --file input.json 
like image 28
Soham Krishna Paul Avatar answered Oct 18 '22 20:10

Soham Krishna Paul