If you use mongodb, you might have the challenge of taking data dump or importing the data dump back to the db. This article is for you. I am currently using Windows OS. For other OS like Mac OS and Linux, the process is almost similar.
The mongorestore program loads data from either a binary database dump created by mongodump or the standard input into a mongod or mongos instance. The mongorestore tool is the part of the mongodb tools package.
First, visit the directory where mongodb is installed locally
cd C:\Program Files\MongoDB\Server\3.4\bin
Now type in the restore command from the command line, whose syntax is this:
mongorestore [options] [<directory>/<BSON file>]
so, it might look something similar, For example : to restore from a dump directory to a local mongod instance running on port 27017:
If you want to restore the dump to a particular database, you can do this :
mongorestore --db=dbname C:\Users\Sujay.Kundu\Downloads\dump
- Some Options :
If you are using a remote db or Mongodb in docker, which requires access authentication, you might need to add –uri:
The following command-line options cannot be used in conjunction with
--password(if the URI connection string also includes the password)
Instead, specify these options as part of your
--uri connection string.
Specifies a resolvable hostname for the
mongod to which to connect. By default, the mongorestore attempts to connect to a MongoDB instance running on the localhost on port number
mongorestore -d dbname --drop C:\Users\Sujay.Kundu\Downloads\dump --uri="mongodb://username:[email protected]:27017/database?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&retryWrites=false&ssl=false"