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


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


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.


# 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 -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 -d my-db 
vote vote


bsondump collection.bson > collection.json 

and then

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


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

vote vote


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