Open In App

How to Use MongoDump for a Single Collection Backup in MongoDB

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Backing up MongoDB data is important for ensuring data integrity and disaster recovery. mongodump is a MongoDB utility that allows us to create backups of our databases.

In this article, we’ll explore how to use Mongodump specifically for backing up a single collection. We’ll cover the concepts behind mongodump, and provide detailed examples to understand the process effectively.

Understanding MongoDump

  • mongodump is a command-line tool provided by MongoDB that creates binary export data dumps of MongoDB databases. It captures data in BSON format preserving data types and structures.
  • By default, mongodump exports all collections in a specified database. However, we can target a specific collection by providing additional parameters.

Using mongodump for a Single Collection Backup

To back up a single collection using mongodump, we will need to specify the database and collection names along with any authentication details if applicable.

Example:

Let’s say we have a MongoDB database named myDatabase with a collection named myCollection. We want to create a backup of only the myCollection collection.

mongodump --host <hostname> --port <port> --username <username> --password <password> --db myDatabase --collection myCollection

Explanation:

  • mongodump: The command to run the mongodump utility.
  • –host <hostname>: Specifies the hostname or IP address of the MongoDB server.
  • –port <port>: Specifies the port number on which MongoDB is running.
  • –username <username>: Specifies the username for authentication (if authentication is enabled).
  • –password <password>: Specifies the password for authentication (if authentication is enabled).
  • –db myDatabase: Specifies the name of the database (myDatabase) containing the collection to be backed up.
  • –collection myCollection: Specifies the name of the collection (myCollection) to be backed up.

Output:

2021-10-01T12:34:56.789+0000    writing myDatabase.myCollection to dump/myDatabase/myCollection.bson
2021-10-01T12:34:56.790+0000 done dumping myDatabase.myCollection (1 document)

After running the mongodump command, you’ll see output indicating the progress of the backup process. If successful, you’ll find the backup files in the default dump directory (typically a dump folder in the current working directory).

Restoring a Single Collection Backup

Once you have created a backup of a single collection using mongodump, you can restore it using the mongorestore utility.

Example:

mongorestore --host <hostname> --port <port> --username <username> --password <password> --db myDatabase --collection myCollection dump/myDatabase/myCollection.bson

Explanation:

  • mongorestore: The command to run the mongorestore utility.
  • –host <hostname>, –port <port>, –username <username>, –password <password>: Similar to mongodump, these parameters specify the connection details for the MongoDB server.
  • –db myDatabase: Specifies the name of the target database (myDatabase) where the collection will be restored.
  • –collection myCollection: Specifies the name of the target collection (myCollection) where the backup data will be restored.
  • dump/myDatabase/myCollection.bson: Specifies the path to the backup file containing the data to be restored.

Output:

After running the mongorestore command, we will see output indicating the progress of the restore process. If successful, the specified collection will be restored in the target database.

Conclusion

Using mongodump to back up a single collection in MongoDB is a straightforward process that requires specifying the database and collection names along with connection details. By understanding the concepts behind mongodump and mongorestore, you can effectively create and restore backups of specific collections, ensuring the safety and integrity of your MongoDB data. Experiment with different options and scenarios to familiarize yourself with the backup and restore process.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads