How to Import MongoDB Data

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.


To import databases with the DigitalOcean Managed Databases MongoDB engine, you need:

  • A MongoDB client, like the mongo Shell or mongoimport which is part of MongoDB Database Tools.

  • The database file stored locally on the same machine as your MongoDB client. If you are experimenting with MongoDB, you can download a sample database file from MongoDB’s official website. Otherwise, see how to export data from your database.

  • The database’s connection details. To get the database’s connection parameters from your control panel, visit the Databases page for your database. On the Overview tab, the Connection Details panel has your Connection string.

Databases Overview screen showing connection string

Import Data

To import data to a MongoDB database, you can use mongoimport to import specific collections data, or you can 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

The mongoimport command imports content from a JSON, CSV, or TSV export to your database. It requires values for the following flags:

  • --uri: The cluster’s connection string to the target database.
  • --collection: The name of the new collection you are creating.

You can find the cluster’s connection string by referencing the connection details of your cluster.

The mongoimport command uses the following syntax:

mongoimport --uri "<cluster-connection-string>"  --collection test  test.json

An example command looks like this:

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 import returns:

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.

Once you have imported the database file, you can verify it imported by connecting to the cluster and running the following command from the MongoDB shell:

show collections

The command returns a list of collection names.

Import with mongorestore

The mongoimport command imports content from a JSON, CSV, or TSV export to your database. It requires values for the following flags:

  • --uri: The cluster’s connection string to the target database.

You can find the cluster’s connection string by referencing the connection details of your cluster.

The mongoimport command uses the following syntax:

mongorestore --uri "<cluster-connection-string>" /path/of/dump

An example command looks like this:

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

A successful import returns:

2022-07-18T14:39:42.151-0400	0 document(s) restored successfully. 0 document(s) failed to restore.

Once you have imported the database file, you can verify it imported by connecting to the cluster and running the following command from the MongoDB shell:

show collections

The command returns a list of collection names.

Export Data

To export data from a MongoDB database, you can use mongoexport to export specific collections data, or you can use mongodump to export a binary (BSON) full database backup.

Export with mongoexport

The mongoexport command produces a JSON, CSV, or TSV export from your database. It requires values for the following flags:

  • --uri: The cluster’s connection string to the target database.
  • --collection: The name of the new collection you are creating.
  • --out: The export’s file format (JSON, CSV, or TSV).

You can find the cluster’s connection string by referencing the connection details of your cluster.

The mongoexport command uses the following syntax:

mongoexport --uri "<cluster-connection-string>" --collection test --out test.json

An example command looks like this:

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:

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

The mongodump command produces a binary (BSON) full backup of your database. It requires values for the following flags:

  • --uri: The cluster’s connection string to the target database.
  • --out: The export’s file format (JSON, CSV, or TSV).

You can find the cluster’s connection string by referencing the connection details of your cluster.

The mongodump command uses the following syntax:

mongodump --uri "<cluster-connection-string>" --out databasedump

An example command looks like this:

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

A successful export returns:

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.