Import Data into MongoDB

Deployment Type:

Author: MongoDB Documentation Team

In this guide, you will use the mongoimport tool distributed with MongoDB to bulk import data into your MongoDB instance.

Time required: 15 minutes

What You’ll Need

If you are using MongoDB in the Cloud (Atlas), you will need to:

Or, if you plan on using a local instance of MongoDB, you will need to:

Check Your Environment

You will need to ensure that your MongoDB instance is running and accessible.

Check that you have an Atlas account and have deployed a MongoDB database cluster. See Create an Atlas Account and Cluster for information on how to login and create a cluster in Atlas.

To make sure that your MongoDB instance is running on Windows, run the following command from the Windows command prompt:

tasklist /FI "IMAGENAME eq mongod.exe"

If a mongod.exe instance is running, you will see something like:

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
mongod.exe                    8716 Console                    1      9,508 K

To make sure your MongoDB instance is running on Mac, run the following command from your terminal:

ps -e | grep 'mongod'

If a mongod instance is running, you will see something like:

89780 ttys026    0:53.48 ./mongod

To make sure your MongoDB instance is running on Linux, run the following command from your terminal:

ps -e| grep 'mongod'

If a mongod instance is running, you will see something like:

89780 ttys026    0:53.48 ./mongod

Procedure

1

Download the sample data.

You will be using a product inventory database for the CRUD Getting Started guides. Download the inventory.crud.json file from GitHub.

Save the downloaded file to a file named inventory.crud.json in your downloads folder. If you wish to save the file somewhere else, you can change the value of the --file option to match your file’s location and name.

2

Import the sample data into the inventory collection.

From your system shell or command prompt, use the mongoimport tool to insert the sample documents into the inventory collection in the test database. mongoimport is packaged with MongoDB and will be in the /bin directory of the MongoDB repository.

By default, mongoimport will import data into an instance of MongoDB on localhost, port 27017. To import data into a MongoDB instance running on a different host or port, specify the hostname or port by including the --host and --port options.

Use the --drop option to drop the collection if it already exists. This ensures that the collection will only contain the data you are importing.

Tip

For non-Atlas instances, replace the <user> and <password> placeholders in the example with your MongoDB username and password. For all instances, update the --file path to point to wherever you have saved your copy of the product inventory database.

Go to cloud.mongodb.com to login to your user account.

In the cluster panel, click the “…” button as pictured below.

../../_images/connect_panel.png

Select Command Line Tools from the menu.

Under the Import and Export Tools header, you will see two copyable command line strings. Copy the one for mongoimport.

../../_images/importstring.png

You can use this command to run mongoimport by replacing the <DATABASE>, <PASSWORD>, <FILETYPE>, <COLLECTION> and <FILE> fields with the appropriate values.

In this case, <DATABASE> is test, <FILETYPE> is JSON, <FILE> is inventory.crud.json (preceded with qualified path), and <COLLECTION> is inventory.

mongoimport --db test --collection inventory ^
          --authenticationDatabase admin --username <user> --password <password> ^
          --drop --file ~\downloads\inventory.crud.json
mongoimport --db test --collection inventory \
       --authenticationDatabase admin --username <user> --password <password> \
       --drop --file ~/Downloads/inventory.crud.json
mongoimport --db test --collection inventory \
          --authenticationDatabase admin --username <user> --password <password> \
          --drop --file ~/Downloads/inventory.crud.json

Summary

If you have successfully completed this guide, you have imported your first MongoDB data. In the next guide, you will retrieve the documents that you just imported.

What’s Next

In this guide, you will retrieve all of the documents in a collection in your MongoDB database.