MongoDump, MongoRestore and MongoImport in MongoDB

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.

MongoRestore

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
PowerShell

Now type in the restore command from the command line, whose syntax is this:

$ mongorestore [options] [<directory>/<BSON file>]
PowerShell

so, it might look something similar, For example : to restore from a dump directory to a local mongod instance running on port 27017:

$ mongorestore  C:\Users\Sujay.Kundu\Downloads\dump
PowerShell

If you want to restore the dump to a particular database, you can do this :

$ mongorestore --db=dbname C:\Users\Sujay.Kundu\Downloads\dump
PowerShell
  • Some Options :
  • –host
  • –help
  • –version
  • –quiet
  • –username
  • –password
  • –authenticationDatabase
  • –authenticationMechanism
  • –uri
  • –port
  • –drop

If you are using a remote db or Mongodb in docker, which requires access authentication, you might need to add –uri:

IMPORTANT

The following command-line options cannot be used in conjunction with --uri option:

Instead, specify these options as part of your --uri connection string.

--host=<hostname><:port>-h=<hostname><:port>

Default: localhost:27017

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 27017.

mongorestore -d dbname --drop  C:\Users\Sujay.Kundu\Downloads\dump --uri="mongodb://username:password@localhost:27017/database?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&retryWrites=false&ssl=false"
PowerShell

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *