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 MongoDB Community Edition.

  • 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, open the database’s More menu, then select Connection details and click Flags.

Databases Overview screen showing connection string

Import with the MongoDB Command Line Client

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:[email protected]" -c contacts  /users/example-user/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.

See MongoDB’s documentation to learn how to query your collections, or for more information on how to import your data.