Import Data into MongoDB¶
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:
- Install MongoDB and start up an instance to which you will connect.
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 Get Started with Atlas for information on how to login and create a cluster in Atlas.
- Windows
- macOS
- Linux
To make sure that your MongoDB instance is running on Windows, run the following command from the Windows command prompt:
If a mongod.exe
instance is running, you will
see something like:
To make sure your MongoDB instance is running on Mac, run the following command from your terminal:
If a mongod
instance is running, you will see
something like:
To make sure your MongoDB instance is running on Linux, run the following command from your terminal:
If a mongod
instance is running, you will see
something like:
Procedure¶
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.
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.

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
.

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
.
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.