How to Import a CSV into MongoDB

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

This article will show you how to import a CSV into MongoDB through the command line and the MongoDB shell. Maybe you’re making the switch to MongoDB for the first time and migrating your data over, or maybe you have data from some other source that you’d like to have in your database, whatever the case we’ll show you how to do it. We’ll do a simple demo with a demo CSV file so you can see how we do it. We’ll be using a small dataset so we can focus entirely on the process of the import.

Prerequisites

  • You should have MongoDB installed.
  • Although not required we recommend you have some experience using the command line.

Importing CSV Data into MongoDB

Let’s jump right into how to import a csv file into MongoDB.

The CSV File

For our demonstration we have kept things very simple and have a csv file products.csv that has some data about the products for a grocery store in a very small town. There are 10 products in this file so we know how many to expect after the import.

Let’s take a look to see what’s in the file: File: products.csv

1
2
3
4
5
6
7
8
9
10
11
ProductName,Price,Quantity
1 Gallon Soy Milk,2.5,12
1 Gallon Almond Milk ,2.75,22
Six Pack Soda,3,3
Chocolate Bar,1,4
Paper Towels,1.25,8
Red Wine,14,3
Salt and Vinegar Chips ,2.25,22
Barbecue Chips,2.25,1
Gatorade,2,21
Breath Mints,0.5,11

Notice that the first row holds the name for the field.

If you want to create this file and follow along please do. You can create the file with whatever method you’re most comfortable and using whatever IDE you’d like.

### Make sure MongoDB is running Now that we have a csv file with the data we’d like to import the next thing you’ll need to do is get MongoDB running. If it is already running you can skip this step.

It’s easy to start MongoDB by executing this command:

1
 mongod

Make sure the process starts, our response looked something like this telling us that it has started and is listening on the default MongoDB port of 27017:

1
2
3
 ...
 2019-06-05T13:34:27.858-0500 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-06-05T13:34:27.861-0500 I NETWORK  [initandlisten] waiting for connections on port 27017

To make this process simpler we’re gonna navigate to the directory where our csv lives.

1
cd ~/Demo-327

Now we can use a ls or tree . to see that our products.csv file lives in this directory

1
2
3
$ tree .
.
└── products.csv

Use the mongoimport command

Now we’ll use the mongoimport command with a few flags to import the data. Let’s take a look at the command and then break down the options afterward:

1
mongoimport --type csv -d test -c products --headerline --drop products.csv

We’ll explain the flags we used but you can also use the help command mongoimport --help to get the same information.

  • –type: The input format to import: json, csv, or tsv. We are using csv so that’s what we specify.
  • -d: Specifies what database to use. We used the test database.
  • -c: Specifies what collection to use. We used a collection called products.
  • –headerline: Specifies that the first row in our csv file should be the field names.
  • –drop: Specifies that we want to drop the collection before importing documents.

Lastly we specify the file which we want to import products.csv. Because it lives in our current directory we don’t have to provide any path but if you are not in the directory you’ll have to provide a path.

This is the response we got:

1
2
3
4
mongoimport --type csv -d test -c products --headerline --drop products.csv
2019-06-05T13:46:00.555-0500    connected to: localhost
2019-06-05T13:46:00.555-0500    dropping: test.products
2019-06-05T13:46:00.611-0500    imported 10 documents

It looks like it worked but we want to be a bit skeptical and verify with our own eyes.

Verify the Import Worked

To verify that the import looked we can use the MongoDB shell to query the collection and actually see the documents in the collection. To open the MongoDB shell simply type the following in the command line:

1
mongo

You’ll then be in the MongoDB prompt and from here we can select the database:

1
2
 >use test
switched to db test

Then you can query all the documents in the products collection like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
>db.products.find()
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab60"),
        "ProductName" : "Paper Towels",
        "Price" : 1.25,
        "Quantity" : 8
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab61"),
        "ProductName" : "1 Gallon Soy Milk",
        "Price" : 2.5,
        "Quantity" : 12
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab62"),
        "ProductName" : "Salt and Vinegar Chips",
        "Price" : 2.25,
        "Quantity" : 22
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab63"),
        "ProductName" : "Red Wine",
        "Price" : 14,
        "Quantity" : 3
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab64"),
        "ProductName" : "Barbecue Chips",
        "Price" : 2.25,
        "Quantity" : 1
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab65"),
        "ProductName" : "Six Pack Soda",
        "Price" : 3,
        "Quantity" : 3
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab66"),
        "ProductName" : "1 Gallon Almond Milk",
        "Price" : 2.75,
        "Quantity" : 22
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab67"),
        "ProductName" : "Chocolate Bar",
        "Price" : 1,
        "Quantity" : 4
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab68"),
        "ProductName" : "Breath Mints",
        "Price" : 0.5,
        "Quantity" : 11
}
{
        "_id" : ObjectId("5cf80de8218efd0aa5e5ab69"),
        "ProductName" : "Gatorade",
        "Price" : 2,
        "Quantity" : 21
}

Now we see with our own eyes that the documents were imported into MongoDB.

Conclusion

In this tutorial we showed you how to import your data from a CSV file into a collection in MongoDB. The process is pretty simple and the mongoimport --help command can really be your friend as the options are concise and well explained. If you have a complex migration or still don’t know how you’ll import your data please don’t hesitate to reach out to us at Object Rocket. Thanks for joining us!

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.