How to Import and Export MongoDB Data
Validated on 23 Mar 2025 • Last edited on 14 May 2026
MongoDB is a source-available cross-platform document-oriented database program for high-volume storage. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.
You can import data into or export data from a DigitalOcean Managed MongoDB cluster using MongoDB Database Tools (mongoimport, mongorestore, mongoexport, mongodump) or the MongoDB Shell.
To move workloads from managed MongoDB to a self-managed cluster (or another provider), use the export procedures on a host that can reach your DigitalOcean cluster, then import the resulting files on the destination with the same tools. Add your export host to trusted sources or connect over your VPC when using private connection strings. Large datasets may require exporting data per database or per collection.
These procedures capture data at the time you run them; they are not continuous replication or built-in live import. For a community walkthrough of streaming workloads with MongoDB and Apache Kafka, including operational best practices, see MongoDB & Kafka: Real-Time Data Streaming.
Prerequisites
Before you begin, make sure you have:
- A MongoDB client, such as the MongoDB Shell or MongoDB Database Tools.
- A database export stored locally on the machine running the client. For testing, download a sample database from MongoDB’s official website, or export data from an existing database.
- The cluster’s connection string. You can find it in the Control Panel on the Overview tab under Connection Details.
Import Data
To import data to a MongoDB database, use mongoimport to import data for specific collections, or use mongorestore to import a binary (BSON) full database backup. The exported database file must be stored locally on the same machine as your client.
Import with mongoimport
Use mongoimport for JSON, CSV, or TSV data. Provide the following flags:
--uri: The target cluster’s connection string--collection: The collection to create or overwrite
mongoimport --uri "<cluster-connection-string>" --collection test test.jsonExample:
mongoimport --uri "mongodb+srv://doadmin:<replace-with-your-password>@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true" --collection test test.jsonA successful run reports the number of imported documents:
2022-07-18T14:39:42.151-0400 connected to: mongodb://db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com:27017/
2022-07-18T14:39:42.151-0400 1 document(s) imported successfully. 0 document(s) failed to import.Verify the import by connecting to the cluster and running the following command from the MongoDB shell:
show collectionsImport with mongorestore
Use mongorestore for BSON dumps created by mongodump. Provide the connection string and the path to the dump:
mongorestore --uri "<cluster-connection-string>" /path/to/dumpExample:
mongorestore --uri "mongodb+srv://doadmin:<replace-with-your-password>@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true" /path/to/dumpThe command outputs restored collections and document counts.
Verify the import by connecting to the cluster and running the following command from the MongoDB shell:
show collectionsExport Data
Use mongoexport to export specific collections, or use mongodump to export a binary (BSON) full database backup.
Export with mongoexport
Use mongoexport to produce JSON, CSV, or TSV output. Provide the following flags:
--uri: The source cluster’s connection string--collection: The collection to export--out: Output file path (for exampletest.jsonfor JSON)
mongoexport --uri "<cluster-connection-string>" --collection test --out test.jsonExample:
mongoexport --uri "mongodb+srv://doadmin:<replace-with-your-password>@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true" --collection test --out test.jsonA successful export returns the number of exported records:
2022-07-18T14:39:42.151-0400 connected to: mongodb://db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com:27017/
2022-07-18T14:39:42.151-0400 exported X recordsExport with mongodump
Use mongodump for full database backups in binary (BSON) format. Provide the following flags:
--uri: The source cluster’s connection string (use your managed cluster URI when exporting from DigitalOcean)--out: The output directory
mongodump --uri "<cluster-connection-string>" --out databasedumpExample:
mongodump --uri "mongodb+srv://doadmin:<replace-with-your-password>@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true" --out databasedumpThe command writes BSON files for each collection:
2022-07-18T14:32:43.032-0400 writing admin.system.users to databasedump/admin/system.users.bson
2022-07-18T14:32:43.140-0400 done dumping admin.system.users (2 documents)
2022-07-18T14:32:43.196-0400 writing admin.system.version to databasedump/admin/system.version.bson
2022-07-18T14:32:43.301-0400 done dumping admin.system.version (2 documents)To upload multiple databases simultaneously, see the mongodump and mongorestore commands. See MongoDB’s documentation to learn more about how to import your data or query your collections.