How to Import .bson file format on mongodb

Mongodb

Mongodb Problem Overview


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?

Mongodb Solutions


Solution 1 - Mongodb

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

Solution 2 - Mongodb

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

Solution 3 - Mongodb

bsondump collection.bson > collection.json

and then

mongoimport -d <dbname> -c <collection> < collection.json

Solution 4 - Mongodb

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/

Solution 5 - Mongodb

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

Solution 6 - 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).

Solution 7 - Mongodb

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"

Solution 8 - Mongodb

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

Solution 9 - Mongodb

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.

Solution 10 - Mongodb

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbinalayView Question on Stackoverflow
Solution 1 - MongodbJerryView Answer on Stackoverflow
Solution 2 - MongodbCameronView Answer on Stackoverflow
Solution 3 - MongodbAnton ShutikView Answer on Stackoverflow
Solution 4 - MongodbLalit SharmaView Answer on Stackoverflow
Solution 5 - Mongodbpuneet goyalView Answer on Stackoverflow
Solution 6 - MongodbLooking ForwardView Answer on Stackoverflow
Solution 7 - MongodbInLawView Answer on Stackoverflow
Solution 8 - MongodbturongView Answer on Stackoverflow
Solution 9 - MongodbJitendra ChandwaniView Answer on Stackoverflow
Solution 10 - MongodbRochanView Answer on Stackoverflow