Migrate MongoDB data from one server to another
MongoDB is a powerful document-oriented database system.
It has an index-based search feature and supports various programming languages.
If you're looking to migrate your MongoDB data from one server to another, one of the easiest ways to do so is by using the mongodump and mongorestore commands.
In this blog post, we'll go over how to use these commands to migrate your data from one MongoDB server to another.
Before we get started, it's important to note that the mongodump and mongorestore commands work with BSON files.
BSON is a binary representation of JSON documents, and it's the native data format for MongoDB.
As such, these commands are only able to migrate data from one MongoDB server to another - they will not work with other types of databases.
Now that we've got that out of the way, let's take a look at how to migrate our MongoDB data from one server to another(server 1 to server 2).
For this we'll make use of mongodump and mongorestore commands.
If you face issues running mongo db commands, checkout this article.
1. mongodump command:
We will use mongodump command to export all data from MongoDB server.
mongodump --uri="mongodb+srv://usename:password@cluster1.address.mongodb.net/myFirstDatabase"
Here username & password corresponds to the username & password of the user who has access to the database(server 1) in MongoDB
![MongoDB username under Database access](https://dog1vxceqefwt.cloudfront.net/blog/mirgrating_mongodb_data/mongodb_access.jpg)
To get your MongoDB cluster address follow the steps below:
a. Click on Database tab under Deployment & select Connect
![MongoDB database tab under deployment with connect button](https://dog1vxceqefwt.cloudfront.net/blog/mirgrating_mongodb_data/mongodb_database_tab.jpg)
b. Now click on "Connect your application"
![Mongodb - connect your app option](https://dog1vxceqefwt.cloudfront.net/blog/mirgrating_mongodb_data/mongodb_connect_your_app.jpg)
c. Now copy the cluster address and use that in the mongodump command mentioned above
![Copy MongoDB cluster address](https://dog1vxceqefwt.cloudfront.net/blog/mirgrating_mongodb_data/copy_cluster_address_mongodb.jpg)
Sample logs:
2022-07-27T12:28:49.863+0530 WARNING: On some systems, a password provided directly in a connection string or using --uri may be visible to system status programs such as `ps` that may be invoked by other users. Consider omitting the password to provide it via stdin, or using the --config option to specify a configuration file with the password. 2022-07-27T12:28:51.978+0530 writing myFirstDatabase.sessions to dump\myFirstDatabase\sessions.bson 2022-07-27T12:28:52.037+0530 done dumping myFirstDatabase.sessions (9 documents) 2022-07-27T12:28:52.066+0530 writing myFirstDatabase.recommendations to dump\myFirstDatabase\recommendations.bson 2022-07-27T12:28:52.080+0530 writing myFirstDatabase.quotes to dump\myFirstDatabase\quotes.bson 2022-07-27T12:28:52.102+0530 writing myFirstDatabase.users to dump\myFirstDatabase\users.bson 2022-07-27T12:28:52.105+0530 writing myFirstDatabase.blogs to dump\myFirstDatabase\blogs.bson 2022-07-27T12:28:52.126+0530 done dumping myFirstDatabase.recommendations (0 documents) 2022-07-27T12:28:52.169+0530 done dumping myFirstDatabase.quotes (60 documents) 2022-07-27T12:28:52.174+0530 done dumping myFirstDatabase.users (1 document) 2022-07-27T12:28:52.329+0530 done dumping myFirstDatabase.blogs (53 documents)
Once the mongodump command has completed, you should have a files inside a directory in your current path.
Here is an example from my mongodump.
![migrated mongodb dump data](https://dog1vxceqefwt.cloudfront.net/blog/mirgrating_mongodb_data/migrating_mongodb_data.jpg)
These files contains all of the data from your MongoDB server.
2. mongorestore command:
We will use mongorestore command to import data that we exported in the last step into the new MongoDB server.
mongorestore --uri="mongodb+srv://username:password@cluster2.address.mongodb.net" dump
Here username & password corresponds to the username & password of the user who has access to the database to which your migrating your data to. (server 2)
Again, similar to earlier, get your MongoDB cluster address by following the steps below:
1. Click on Database tab under Deployment & select Connect
2. Now click on "Connect your application"
3. Now copy the cluster address and use that in the mongodump command mentioned above
Sample logs:
2022-07-27T13:24:36.239+0530 WARNING: On some systems, a password provided directly in a connection string or using --uri may be visible to system status programs such as `ps` that may be invoked by other users. Consider omitting the password to provide it via stdin, or using the --config option to specify a configuration file with the password. 2022-07-27T13:24:39.142+0530 preparing collections to restore from 2022-07-27T13:24:39.145+0530 reading metadata for myFirstDatabase.blogs from dump\myFirstDatabase\blogs.metadata.json 2022-07-27T13:24:39.148+0530 reading metadata for myFirstDatabase.quotes from dump\myFirstDatabase\quotes.metadata.json 2022-07-27T13:24:39.152+0530 reading metadata for myFirstDatabase.recommendations from dump\myFirstDatabase\recommendations.metadata.json 2022-07-27T13:24:39.154+0530 reading metadata for myFirstDatabase.sessions from dump\myFirstDatabase\sessions.metadata.json 2022-07-27T13:24:39.156+0530 reading metadata for myFirstDatabase.users from dump\myFirstDatabase\users.metadata.json 2022-07-27T13:24:39.690+0530 restoring myFirstDatabase.blogs from dump\myFirstDatabase\blogs.bson 2022-07-27T13:24:40.822+0530 restoring myFirstDatabase.sessions from dump\myFirstDatabase\sessions.bson 2022-07-27T13:24:40.889+0530 restoring myFirstDatabase.quotes from dump\myFirstDatabase\quotes.bson 2022-07-27T13:24:40.929+0530 finished restoring myFirstDatabase.blogs (53 documents, 0 failures) 2022-07-27T13:24:40.930+0530 restoring to existing collection myFirstDatabase.recommendations without dropping 2022-07-27T13:24:40.932+0530 restoring myFirstDatabase.users from dump\myFirstDatabase\users.bson 2022-07-27T13:24:40.937+0530 restoring myFirstDatabase.recommendations from dump\myFirstDatabase\recommendations.bson 2022-07-27T13:24:40.958+0530 finished restoring myFirstDatabase.recommendations (0 documents, 0 failures) 2022-07-27T13:24:41.060+0530 finished restoring myFirstDatabase.sessions (9 documents, 0 failures) 2022-07-27T13:24:41.179+0530 finished restoring myFirstDatabase.users (1 document, 0 failures) 2022-07-27T13:24:41.364+0530 finished restoring myFirstDatabase.quotes (60 documents, 0 failures) 2022-07-27T13:24:41.364+0530 restoring indexes for collection myFirstDatabase.sessions from metadata 2022-07-27T13:24:41.366+0530 index: &idx.IndexDocument{Options:primitive.M{"expireAfterSeconds":0, "name":"expires_1", "v":2}, Key:primitive.D{primitive.E{Key:"expires", Value:1}}, PartialFilterExpression:primitive.D(nil)} 2022-07-27T13:24:41.370+0530 restoring indexes for collection myFirstDatabase.users from metadata 2022-07-27T13:24:41.371+0530 restoring indexes for collection myFirstDatabase.blogs from metadata 2022-07-27T13:24:41.372+0530 restoring indexes for collection myFirstDatabase.quotes from metadata 2022-07-27T13:24:41.374+0530 index: &idx.IndexDocument{Options:primitive.M{"background":true, "name":"auth.email.verificationToken_1", "unique":true, "v":2}, Key:primitive.D{primitive.E{Key:"auth.email.verificationToken", Value:1}}, PartialFilterExpression:primitive.D(nil)} 2022-07-27T13:24:41.375+0530 index: &idx.IndexDocument{Options:primitive.M{"background":true, "name":"slug_1", "unique":true, "v":2}, Key:primitive.D{primitive.E{Key:"slug", Value:1}}, PartialFilterExpression:primitive.D(nil)} 2022-07-27T13:24:41.376+0530 index: &idx.IndexDocument{Options:primitive.M{"background":true, "name":"slug_1", "unique":true, "v":2}, Key:primitive.D{primitive.E{Key:"slug", Value:1}}, PartialFilterExpression:primitive.D(nil)} 2022-07-27T13:24:41.378+0530 index: &idx.IndexDocument{Options:primitive.M{"background":true, "name":"handle_1", "unique":true, "v":2}, Key:primitive.D{primitive.E{Key:"handle", Value:1}}, PartialFilterExpression:primitive.D(nil)} 2022-07-27T13:24:41.786+0530 restoring indexes for collection myFirstDatabase.recommendations from metadata 2022-07-27T13:24:41.787+0530 index: &idx.IndexDocument{Options:primitive.M{"background":true, "name":"slug_1", "unique":true, "v":2}, Key:primitive.D{primitive.E{Key:"slug", Value:1}}, PartialFilterExpression:primitive.D(nil)} 2022-07-27T13:24:42.099+0530 123 document(s) restored successfully. 0 document(s) failed to restore.
Once the mongorestore command has completed, your data should be imported into the my_database database on the MongoDB server.
And that's all there is to migrating MongoDB data from one server to another!
Using the mongodump and mongorestore commands is a quick and easy way to migrate your data, and it's a great solution if you're looking to move your data to a new server or create a backup.
Bonus:
Here are a few more ways in which you can export your data.
You can export MongoDB data in JSON, CSV, or BSON/mongodump format.
JSON:
The JSON format is the most common format for data exchange on the web. To export MongoDB data in JSON format, use the following command:
mongoexport --db <database_name> --collection <collection_name> --out <filename>.json
CSV:
The CSV (comma-separated values) format is a popular format for data exchange. To export MongoDB data in CSV format, use the following command:
mongoexport --db <database_name> --collection <collection_name> --type=csv --fields <field1>,<field2>,... --out <filename>.csv
BSON/mongodump:
BSON is a binary representation of JSON documents. The mongodump utility can be used to create a BSON file. To export MongoDB data in BSON format, use the following command:
mongodump --db <database_name> --collection <collection_name> --out <filename>
The above commands will export the data from the specified MongoDB collection in the specified format.
![Anil Rao K](https://dog1vxceqefwt.cloudfront.net/public/images/anil_rao_k.jpg)
Author: Anil Rao K