# 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](https://www.mongodb.com/docs/mongodb-shell/) or [`mongoimport`](https://www.mongodb.com/try/download/database-tools) 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](https://docs.atlas.mongodb.com/sample-data/available-sample-datasets) from MongoDB’s official website. Otherwise, see how to [export data from your database](#export-data). - The database’s connection details. To get the database’s connection parameters from your control panel, visit [the Databases page](https://cloud.digitalocean.com/databases) for your database. On the **Overview** tab, the Connection Details panel has your **Connection string**. ![Databases Overview screen showing connection string](https://docs.digitalocean.com/screenshots/databases/mongodb-connection-details.172d40650dd0c1a55ab20b9a47e443a62548e3f6599ff0ce5dedbe252a52f836.png) ## Import Data To import data to a MongoDB database, you can [use `mongoimport`](#import-with-mongoimport) to import specific collections data, or you can [use `mongorestore`](#import-with-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](https://docs.digitalocean.com/products/databases/mongodb/how-to/connect/index.html.md#connection-details) of your cluster. The `mongoimport` command uses the following syntax: ```shell mongoimport --uri "" --collection test test.json ``` An example command looks like this: ```shell mongoimport --uri "mongodb+srv://doadmin:@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: ```shell 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](https://docs.digitalocean.com/products/databases/mongodb/how-to/connect/index.html.md#connection-details) and running the following command from the MongoDB shell: ```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](https://docs.digitalocean.com/products/databases/mongodb/how-to/connect/index.html.md#connection-details) of your cluster. The `mongoimport` command uses the following syntax: ```shell mongorestore --uri "" /path/of/dump ``` An example command looks like this: ```shell mongorestore --uri "mongodb+srv://doadmin:@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: ```shell 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](https://docs.digitalocean.com/products/databases/mongodb/how-to/connect/index.html.md#connection-details) and running the following command from the MongoDB shell: ```shell show collections ``` The command returns a list of collection names. ## Export Data To export data from a MongoDB database, you can [use `mongoexport`](#export-with-mongoexport) to export specific collections data, or you can [use `mongodump`](#export-with-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](https://docs.digitalocean.com/products/databases/mongodb/how-to/connect/index.html.md#connection-details) of your cluster. The `mongoexport` command uses the following syntax: ```shell mongoexport --uri "" --collection test --out test.json ``` An example command looks like this: ```shell mongoexport --uri "mongodb+srv://doadmin:@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: ```shell 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](https://docs.digitalocean.com/products/databases/mongodb/how-to/connect/index.html.md#connection-details) of your cluster. The `mongodump` command uses the following syntax: ```shell mongodump --uri "" --out databasedump ``` An example command looks like this: ```shell mongodump --uri "mongodb+srv://doadmin:@db-mongodb-nyc3-73883-4aef1b0f.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-nyc3-73883&tls=true" --out databasedump ``` A successful export returns: ```shell 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`](https://www.mongodb.com/docs/database-tools/mongodump/) and [`mongorestore`](https://www.mongodb.com/docs/database-tools/mongorestore/) commands. See MongoDB’s documentation to learn [more about how to import your data](https://docs.mongodb.com/database-tools/mongoimport/) or [query your collections](https://docs.mongodb.com/manual/tutorial/query-documents/).