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.

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

Verify imported documents

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

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.

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