How to Import MongoDB Collections

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 will 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 in JSON, CSV, SQL, or BSON format. If you are experimenting with MongoDB, you can download a sample database file from MongoDB’s official website.

  • 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 with mongoimport

To import a database with the MongoDB command line client, the exported database file must be stored locally on the same machine as your client.

To import the database, use the mongoimport command. The mongoimport command requires the cluster’s connection string to the target database and values for the following flags:

  • --uri: The cluster’s connection string to the target database.
  • -c: 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>" -c <name-of-collection> <local-path-to-file>

An example command looks like this:

mongoimport --uri "mongodb+srv://doadmin:<replace-with-your-password>" -c contacts  /home/example/database-file.json

A successful import returns:

2021-02-08T11:14:03.247-0500	connected to: mongodb://
2021-02-08T11:14:03.377-0500	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.

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.