Import and export tools
The tools in this group are used to export or restore files from the database. Mainly, components in this category are classified into two different groups. This includes components that work with binary data or interact with common data formats such as JSON, CSV, and so on.
Using import and export tools
In order to import or export binary data from and into the database, developers can use tools in this group. We use tools and utilities in this category to create or restore backups for our MongoDB server. In further chapters, we will discuss the backup strategies in detail.
The following are the tools that are available to perform these kinds of tasks:
mongodump
mongorestore
bsondump
mongooplog
Understanding mongodump
This process comes in handy when developers or system administrators want to create a dump file from a database in the binary format. This utility and other related tools are useful for MongoDB backup strategies.
The mongodump
process is able to retrieve data from either mongod
or mongos
processes.
Note
For more information on mongodump, visit the MongoDB documentation page at http://docs.mongodb.org/manual/reference/program/mongodump/.
Utilizing mongorestore
This process is used to restore and write the binary files that are generated by the mongodump
process into the database server. To restore data, mongorestore
can establish a new database or use an existing database. Just like the mongodump
instance, mongorestore
can connect to a mongos
instance or it can connect directly to the mongod
process.
Note
To read more about mongostore
, you can visit the MongoDB documentation page at http://docs.mongodb.org/manual/reference/program/mongorestore/.
Learning about bsondump
The bsondump
process is used to convert the BSON format data to common data formats such as JSON. Essentially, bsondump
comes in handy when developers want to convert dump files that are generated by mongodump
to human-readable formats.
A very simple usage of this command is shown in the following command line:
bsondump data.bson>data.json
Understanding mongooplog
The mongooplog
is a utility that duplicates oplog
from one server to another server, for instance, in order to perform a migration task. In order to perform the migration operation, mongooplog
accepts two parameters, the from
and to
server addresses.
Note
What is oplog?
The oplog
or operation log is a capped collection (a fixed-sized collection) of data that keeps the record of all data altering operations. In further chapters, we will explain this feature in detail.
The following is a simple usage of this command:
mongooplog --from server1 --host server2
The preceding command line will connect to the MongoDB instance of server1
and copy the entire oplog
to server2
instance.
Using data tools
In this group, we have two utilities that help us generate or import data in human-readable formats such as JSON or CSV into or from the MongoDB instance. These are mentioned in the following lists:
mongoimport
mongoexport
Understanding mongoexport
In order to export data in JSON or CSV formats from the MongoDB instance, developers can use this utility.
The following is a simple usage of this command:
mongoexport --db mydb --collection posts --out export.json
The preceding command line will connect to a local instance of MongoDB, retrieve all records from the posts
collection of the mydb
database in the JSON format, and write all outputs to the export.json
file.
Utilizing mongoimport
The mongoimport
utility can help you import the produced export files in JSON, CSV or TSV formats into the MongoDB instance. The export files can be generated from either mongoexport
or from other third-party export tools.
The following example is a basic usage of this command:
mongoimport --db mydb --collection posts --file export.json
The preceding command line will import the export.json
entries into the posts
collection of the mydb
database. The same instruction can be used for other data formats using the --type
option.