How to Import .bson file format on mongodb

ID : 20204

viewed : 15

Tags : mongodbmongodb

Top 5 Answer for How to Import .bson file format on mongodb

vote vote

95

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 
vote vote

88

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 
vote vote

74

bsondump collection.bson > collection.json 

and then

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

63

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/

vote vote

53

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

Top 3 video Explaining How to Import .bson file format on mongodb

Related QUESTION?