import csv using mongoimport

How to import CSV using mongoimport command in MongoDB

Are you struggling to import CSV file data using mongoimport command in MongoDB ? Then this tutorial will teach you how to do that. The prerequisites to try out this example is, please ensure you had installed MongoDB server and mongoimport is available in the bin directory as shown below.

import csv using mongoimport

Import CSV using mongoimport

To import CSV file first you need to copy the CSV file in some location. For simplicity let’s copy the CSV file (items.csv) in “bin” directory as shown above. For example, the contents of CSV file looks like the following. There are three records in this file.

1,Lenova E4430,2,98000
2,Mac Pro Desktop,1,95456
3,Microsoft LAM-00001 Surface Studio 2,1,910000

Now open the command prompt and run the following mongoimport command from “bin” directory. You should see successful message like “imported 3 documents

> mongoimport -d sneppets -c items --type csv --file items.csv --headerline

2019-04-06T14:29:29.165+0530    connected to: localhost
2019-04-06T14:29:29.174+0530    imported 3 documents

Verify imported documents

To verify imported document, first switch to mongo shell and follow the below steps

C:\Program Files\MongoDB\Server.0\bin>mongo
MongoDB shell version v4.0.6
connecting to: mongodb://
Implicit session: session { "id" : UUID("91791660-40da-47ae-86fc-9cd05a05271c")
MongoDB server version: 4.0.6
Server has startup warnings:
2019-04-06T13:22:39.369+0530 I CONTROL  [initandlisten]
2019-04-06T13:22:39.369+0530 I CONTROL  [initandlisten] ** WARNING: Access contr
ol is not enabled for the database.
2019-04-06T13:22:39.369+0530 I CONTROL  [initandlisten] **          Read and wri
te access to data and configuration is unrestricted.
2019-04-06T13:22:39.369+0530 I CONTROL  [initandlisten]
Enable MongoDB's free cloud-based monitoring service, which will then receive an
d display
metrics about your deployment (disk utilization, CPU, operation statistics, etc)

The monitoring data will be available on a MongoDB website with a unique URL acc
essible to you
and anyone you share the URL with. MongoDB may use this information to make prod
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeM


Then switch to your database (sneppets) and run the following mongo query on the collection “items“. Please note that I have created collection called “items” and thats where we tried to import data from CSV file.

> use sneppets

switched to db sneppets

> db.items.find()

{ "_id" : ObjectId("5ca86a71d9caf05c4d9af01e"), "id" : 1, "item_name" : "Lenova
E4430", "qty" : 2, "price" : 98000 }
{ "_id" : ObjectId("5ca86a71d9caf05c4d9af01f"), "id" : 2, "item_name" : "Mac Pro
 Desktop", "qty" : 1, "price" : 95456 }
{ "_id" : ObjectId("5ca86a71d9caf05c4d9af020"), "id" : 3, "item_name" : "Microso
ft LAM-00001 Surface Studio 2", "qty" : 1, "price" : 910000 }

Now you have verified and three documents were imported. That’s all.

Import CSV using MongoDB Compass

Additionally if you had installed MongoDB compass Community as shown below, you can import CSV data easily through this GUI interface and verify the results very quick.

mongodb csv import mongo compass

Further Learning


Leave a Reply

Notify of