I've exported the database on the server using mongodump
command and dump is stored in .bson
file. I need to import that in my local server using mongorestore
command. However it's not working. What is the correct mongorestore
command and what are the other tools to restore db
?
This question is related to
mongodb
You have to run this mongorestore command via cmd and not on Mongo Shell... Have a look at below command on...
Run this command on cmd (not on Mongo shell)
>path\to\mongorestore.exe -d dbname -c collection_name path\to\same\collection.bson
Here path\to\mongorestore.exe
is path of mongorestore.exe
inside bin folder of mongodb. dbname is name of databse. collection_name
is name of collection.bson. path\to\same\collection.bson
is the path up to that collection.
Now from mongo shell you can verify that database is created or not (If it does not exist, database with same name will be created with collection).
mongorestore
is the tool to use to import bson files that were dumped by mongodump
.
From the docs:
mongorestore takes the output from mongodump and restores it.
Example:
# On the server run dump, it will create 2 files per collection
# in ./dump directory:
# ./dump/my-collection.bson
# ./dump/my-collection.metadata.json
mongodump -h 127.0.0.1 -d my-db -c my-collection
# Locally, copy this structure and run restore.
# All collections from ./dump directory are picked up.
scp user@server:~/dump/**/* ./
mongorestore -h 127.0.0.1 -d my-db
mongorestore -d db_name /path/
make sure you run this query in bin folder of mongoDb
C:\Program Files\MongoDB\Server\4.2\bin -
then run this above command.
I have used this:
mongorestore -d databasename -c file.bson fullpath/file.bson
1.copy the file path and file name from properties (try to put all bson files in different folder), 2.use this again and again with changing file name only.
In mongodb 3.0 or above, we can specify database name to restore. Assuming that you are standing at the root directory that contains bson files
./
a.bson
b.metadata.bson
...
The script would be
for FILENAME in *; do mongorestore -d <db_name> -c "${FILENAME%.*}" $FILENAME; done
Best,
Run the following from command line and you should be in the Mongo bin directory.
mongorestore -d db_name -c collection_name path/file.bson
It's very simple to import a .bson file:
mongorestore -d db_name -c collection_name /path/file.bson
Incase only for a single collection.Try this:
mongorestore --drop -d db_name -c collection_name /path/file.bson
For restoring the complete folder exported by mongodump
:
mongorestore -d db_name /path/
If your access remotely you can do it
for bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people "/home/${USER}/people.bson"
for bson compressed in .gz (gzip) format:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people --gzip --dir "/home/${USER}/people.bson.gz"
Just for reference if anyone is still struggling with mongorestore.
You have to run monogorestore in terminal/command prompt and not in mongo console.
$ mongorestore -d db_name /path_to_mongo_dump/
for more details you can visit official documentations
https://docs.mongodb.com/manual/reference/program/mongorestore/
bsondump collection.bson > collection.json
and then
mongoimport -d <dbname> -c <collection> < collection.json
Source: Stackoverflow.com