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:

Databases Overview screen showing connection string

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.json

Example:

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.json

A 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 collections

Import 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/dump

Example:

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/dump

The 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 collections

Export 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 example test.json for JSON)
mongoexport --uri "<cluster-connection-string>" --collection test --out test.json

Example:

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.json

A 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 records

Export 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 databasedump

Example:

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 databasedump

The 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.

We can't find any results for your search.

Try using different keywords or simplifying your search terms.