MongoDB Binary Import and Export Tools

Soni Pandey
3 min readSep 19, 2016

--

Use mongodump and mongorestore in place of mongoexport and mongoimport respectively.
Note: Do not use mongoimport and mongoexport for full-scale backups because they may not reliably capture data type information. Use mongodump and mongorestore as described in “Backup Strategies for MongoDb Systems” for this kind of functionality.

Use mongodump to export data:

mongodump is a utility for creating a binary export of the contents of a database. Consider using this utility as part of an effective backup strategy. Use mongodump in conjunction with mongorestore to restore databases.

mongodump can read data from either mongod or mongos instances, in addition to reading directly from MongoDB data files without an active mongod.

mongodump — host localhost — port 27017
mongodump — host localhost — port 27017 — db database_name — out /data/backup/

Here, we used — host, — port, — db, — out options.
— host <hostname><:port>, -h <hostname><:port>: Default: localhost:27017, Specifies a resolvable hostname for the mongod to which to connect. By default, the mongodump attempts to connect to a MongoDB instance running on the localhost on port number 27017.

— port <port>: Default: 27017, Specifies the TCP port on which the MongoDB instance listens for client connections.

— db <database>, -d <database>: Specifies a database to backup. If you do not specify a database, mongodump copies all databases in this instance into the dump files.

— out <path>, -o <path>: Specifies the directory where mongodump will write BSON files for the dumped databases. By default, mongodump saves output files in a directory named dump in the current working directory.

To send the database dump to standard output, specify “-” instead of a path. Write to standard output if you want process the output before saving it, such as to use gzip to compress the dump. When writing standard output, mongodump does not write the metadata that writes in a <dbname>. metadata.json file when writing to files directly.

There are many options, you can have a look here.

Use mongorestore to import data:

The mongorestore program writes data from a binary database dump created by mongodump to a MongoDB instance. mongorestore can create a new database or add data to an existing database.

mongorestore can write data to either mongod or mongos instances, in addition to writing directly to MongoDB data files without an active mongod.

mongorestore — host localhost — port 27017 data\backup(path of database backup)
mongorestore — host localhost — port 27017 — db database_name data/backup(path of database backup)

Here, we used — host, — port, — db options.
— 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.

— port <port>: Default: 27017, Specifies the TCP port on which the MongoDB instance listens for client connections.

— db <database>, -d <database>: Specifies a database for mongorestore to restore data into. If the database does not exist, mongorestore creates the database. If you do not specify a <db>, mongorestore creates new databases that correspond to the databases where data originated and data may be overwritten. Use this option to restore data into a MongoDB instance that already has data.

— db does not control which BSON files mongorestore restores. You must use the mongorestore path option to limit that restored data.

There are many options, you can have a look here.

You can reach out to me for any doubt and suggestions. Please share this with others as well.

Oh, and if you like this, click the 💚 below. Keep watching this space and follow me for more articles on technology.

Thanks!
Happy Coding!!

--

--

Soni Pandey
Soni Pandey

Written by Soni Pandey

I am a Node.js Developer and eager to learn new technology. I blog, tweet & read whenever I can.

Responses (1)