How to restore the dump into your running mongodb
Asked Answered
H

14

85

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.

Hagiographa answered 21/9, 2013 at 10:43 Comment(9)
God knows why exactly you are getting this error. But make sure you are having mongod running in background. Though it has nothing to with this error. By the way did you manually changed the dump files?Happily
No I downloaded the assignment dump file. and extracted it.If you have done earlier can you please give me steps.Hagiographa
Are you doing that mongodb 101 course? I also did that, it worked for me. I hope you have the binaries of mongorestore of a stable mongodb release.Happily
yes, I am doing the same. might me I am going it in wrong way. could you please give me way, where to put dump.bson. my db path is dbpath=C:\mongodb\data\db\Hagiographa
Actually I use Mac OS X. Really don't know much about Windows. Try following the instructions they give for windows. Or better discuss at their course forum. Many might have faced similar problem. Tension not, ho jayega :)Happily
Same query exactly what the sh*t i needed . Thanks for asking the query it helps !!Lateral
@SandeepSingh Suggestion - Its always good to accept an answer. People looking for a solution to the same problem can just see that right up as the first answer.Clere
@Clere - Yes you are correct, certainly I missed to accept the right answer as there was not any answer when I posted it. Now it will very unfair for others, if I am going to accept any answer from this. :)Hagiographa
@SandeepSingh you can always choose what is the most reasonably explained and useful answer according to you. (despite the votes) ..breaking a tie could also happen using the time when the answer was posted. <= opinionsClere
W
117

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.

Windowsill answered 20/1, 2015 at 11:49 Comment(3)
How to pass the DB credentials when restoring?Recoverable
@Recoverable mongorestore -u <username> --authenticationDatabase "admin" --db databasename. Authentication database not necessarily 'admin', but probably. You should be asked for the password interactively (you may also need to elevate with sudo).Atelectasis
it should be --db or -d but not -dbCircumambulate
W
59

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 row will be used 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
Witkin answered 13/9, 2017 at 4:55 Comment(0)
C
31

To restore a single database:

  1. Backup the testdb database

    $ mongodump --db testdb
    
  2. Restore the testdb database to a new database called testdb2

    $ mongorestore --db testdb2 dump/testdb
    

To restore all databases:

  1. Backup all databases

    $ mongodump
    
  2. Restore all databases

    $ mongorestore dump
    
Civilly answered 5/7, 2019 at 19:58 Comment(0)
T
14

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.

Thief answered 14/1, 2014 at 6:17 Comment(1)
You should be sure before you use --drop in your command cause it will drop the existing collection in the DB and recreate a new collection with the data from the dump file, technically you will loose all the current data(which is not in dump file) from DB in mongo instance..!!!Ileana
M
9

Follow this path.

C:\Program Files\MongoDB\Server\4.2\bin

Run 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
Mchenry answered 16/12, 2019 at 8:45 Comment(0)
H
5

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/directory

You 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>
Hinshelwood answered 20/9, 2018 at 20:57 Comment(0)
L
4
  1. Start mongod
  2. Navigate to folder where you have extracted "enron.zip" in OS shell(cmd in case of windows)
  3. Then type ">mongorestore -d your_db_name dump/enron"
Loginov answered 4/5, 2015 at 16:51 Comment(0)
A
3

For mongoDB database restore use this command here

mongorestore --db databasename --drop <dump/file/path>
Ashworth answered 3/10, 2018 at 12:12 Comment(1)
You should be sure before you use --drop in your command cause it will drop the existing collection in the DB and recreate a new collection with the data from the dump file, technically you will loose all the current data(which is not in dump file) from DB in mongo instance..!!!Ileana
A
2

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=$restoreFileName

If your database is running on localhost on default port without authentication then the following will work.

$ mongorestore -v --gzip --archive=restorefile.gzip --drop

More details - mongorestore

Ambidextrous answered 11/3, 2021 at 7:35 Comment(0)
Y
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.

Yungyunick answered 7/4, 2020 at 9:8 Comment(0)
F
0

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
Fiveandten answered 1/5, 2019 at 14:2 Comment(0)
M
0

mongodump --host test.mongodb.net --port 27017 --db --username --password --authenticationDatabase admin --ssl --out

mongorestore --db --verbose

Melainemelamed answered 10/6, 2019 at 5:44 Comment(0)
F
0

Process of Backup MongoDb Collections.

  1. Find the MongoDb folder in ProgramFiles or ProgramFiles32.
  2. Navigate to bin Folder.Find the path like this.C:\Program Files\MongoDB\Server\4.2\bin
  3. Make you have to Install Mongo Dump Tools. enter image description here
  4. If you are not find any such tools , kindly go through this link and install it. MongDb Database Tools
  5. inside bin Folder open CMD or navigate to MongdDb Server Bin folder.
  6. 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

  1. open the CMD prompt in Mongdb Server bin Folder.

  2. after that Run this Command **mongorestore --db mydababse --verbose

  3. D:\MongoDbBackup\mydababse\Dec28-21**(this this path where I have store my backup collection).

  4. some time mongDb Restore program required folder permission to restore.

Force answered 29/12, 2021 at 7:2 Comment(0)
A
-1

For mongoDB database restore use this command here . First go to your mongodb database location such as For Example : cd Downloads/blank_db/v34000 After that Enter mongorestore -d v34000 ./

Abundant answered 18/9, 2019 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.