How to use the command "mongodump" (with examples)
The “mongodump” command is a utility provided by MongoDB that allows you to export the contents of a MongoDB instance. It can be used to create dumps of databases or specific collections, and supports various options to customize the backup process.
Use case 1: Create a dump of all databases
Code:
mongodump
Motivation: One common use case for mongodump is creating a backup of all the databases in your MongoDB instance. This is useful for situations like migrating data to a different server or recovering from data loss. By running the mongodump
command without any additional arguments, it will create a dump of all the databases and place the dump files inside a directory called “dump”.
Explanation: The command mongodump
without any arguments will create a dump of all the databases in the MongoDB instance. It will connect to the default host and port (localhost:27017) and create a directory named “dump” in the current working directory. Inside the “dump” directory, separate subdirectories will be created for each database, containing the dump files.
Example output: The command will generate a directory structure as follows:
dump/
database1/
collection1.bson
collection2.bson
database2/
collection3.bson
Use case 2: Specify an output location for the dump
Code:
mongodump --out path/to/directory
Motivation: Sometimes, you may want to save the dump files to a specific directory rather than the default “dump” directory. The --out
option allows you to specify the output location for the dump.
Explanation: The --out
option is followed by the path to the directory where you want the dump files to be saved. When you provide this option, mongodump
will create the specified directory if it doesn’t exist and save the dump files inside it.
Example output: If you run the command mongodump --out /path/to/mybackup
, the dump files will be saved in the /path/to/mybackup
directory.
/path/to/mybackup/
database1/
collection1.bson
collection2.bson
database2/
collection3.bson
Use case 3: Create a dump of a given database
Code:
mongodump --db database_name
Motivation: In some cases, you may only need to create a backup of a specific database rather than all the databases in the MongoDB instance. The --db
option allows you to specify the name of the database you want to dump.
Explanation: The --db
option is followed by the name of the database you want to create a dump for. When you provide this option, mongodump
will only dump the specified database.
Example output: If you run the command mongodump --db mydatabase
, it will create a dump of only the “mydatabase” database.
dump/
mydatabase/
collection1.bson
collection2.bson
Use case 4: Create a dump of a given collection within a given database
Code:
mongodump --collection collection_name --db database_name
Motivation: MongoDB collections can have a large number of documents, and in some cases, you may only need a backup of a specific collection within a database. The --collection
option allows you to specify the name of the collection you want to dump, along with the name of the database.
Explanation: The --collection
option is followed by the name of the collection you want to create a dump for, and the --db
option is followed by the name of the database that contains the collection. When you provide these options, mongodump
will only dump the specified collection within the specified database.
Example output: If you run the command mongodump --collection mycollection --db mydatabase
, it will create a dump of only the “mycollection” collection within the “mydatabase” database.
dump/
mydatabase/
mycollection.bson
Use case 5: Connect to a given host running on a given port, and create a dump
Code:
mongodump --host host --port port
Motivation: By default, mongodump
connects to the MongoDB instance running on localhost at port 27017. However, in some cases, you may need to connect to a different host and port to create a dump. The --host
and --port
options allow you to specify the host and port to connect to.
Explanation: The --host
and --port
options are followed by the hostname or IP address and the port number of the MongoDB instance you want to connect to. When you provide these options, mongodump
will connect to the specified host and port to create the dump.
Example output: If you run the command mongodump --host myserver --port 27018
, it will connect to the MongoDB instance running on “myserver” at port 27018 and create the dump.
dump/
database1/
collection1.bson
collection2.bson
Use case 6: Create a dump of a given database with a given username; user will be prompted for password
Code:
mongodump --username username --password --db database
Motivation: In MongoDB, databases can be secured with authentication, and you may need to provide a username and password to connect to a database. The --username
option allows you to specify the username, and --password
prompts you to enter the password for the given username.
Explanation: The --username
option is followed by the username you want to use to authenticate with the MongoDB instance. The --password
option is used to prompt the user for the password, providing an extra layer of security. Lastly, the --db
option is followed by the name of the database you want to create a dump for.
Example output: If you run the command mongodump --username john --password --db mydatabase
, it will prompt you to enter the password for the “john” user and create a dump of the “mydatabase” database.
dump/
mydatabase/
collection1.bson
collection2.bson
Use case 7: Create a dump from a specific instance; host, user, password, and database will be defined in the connection string
Code:
mongodump --uri connection_string
Motivation: In some cases, you may have a connection string that includes the hostname, port, username, password, and database information. The --uri
option allows you to provide the connection string instead of specifying each individual option separately.
Explanation: The --uri
option is followed by the connection string containing all the necessary information to connect to the MongoDB instance, including the hostname, port, username, password, and database. When you provide this option, mongodump
will parse the connection string and create the dump accordingly.
Example output: If you run the command mongodump --uri "mongodb://john:password@myserver:27018/mydatabase"
, it will connect to the MongoDB instance running on “myserver” at port 27018 using the “john” user with the given password, and create a dump of the “mydatabase” database.
dump/
mydatabase/
collection1.bson
collection2.bson
Conclusion:
The mongodump
command is a powerful utility for creating backups of MongoDB databases and collections. It provides a variety of options to customize the backup process, allowing you to create dumps of all databases, specific databases or collections, and connect to different hosts and ports. Whether you need to migrate data, recover from data loss, or simply create regular backups, the mongodump
command is an essential tool for managing your MongoDB instances.