How to Import .bson file format on 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
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
bsondump collection.bson > collection.json
and then
mongoimport -d <dbname> -c <collection> < collection.json
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/