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
?
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/
Note: If you have enabled authentication use the below syntax:
mongorestore -u username --authenticationDatabase admin -d db_name -c collection_name /path/file.bson
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
dump/dbName/collectionName.bson
folder structure? I used mongodump
, but now I want to import it using mongorestore
on a remote Linux box.
mongorestore --collection people --db accounts myDump/accounts/people.bson
bsondump collection.bson > collection.json
and then
mongoimport -d <dbname> -c <collection> < collection.json
mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.
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/
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
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).
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"
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
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.
Success story sharing
mongoexport
if you're only exporting single collections.mongoimport
was giving me "invalid character" errors. Thanks!Mongorestore -h [host] -u [user] -p [pass] -d [database] -c [collection] [bson file]