[database] How to use mongoimport to import csv

Trying to import a CSV with contact information:

Name,Address,City,State,ZIP  
Jane Doe,123 Main St,Whereverville,CA,90210  
John Doe,555 Broadway Ave,New York,NY,10010 

Running this doesn't seem to add any documents to the database:

$ mongoimport -d mydb -c things --type csv --file locations.csv --headerline

Trace says imported 1 objects, but firing up the Mongo shell and running db.things.find() doesn't show any new documents.

What am I missing?

This question is related to database mongodb csv import mongoimport

The answer is


When I was trying to import the CSV file, I was getting an error. What I have done. First I changed the header line's column names in Capital letter and removed "-" and added "_" if needed. Then Typed below command for importing CSV into mongo

$ mongoimport --db=database_name --collection=collection_name --type=csv --file=file_name.csv --headerline  

enter image description here


use :

mongoimport -d 'database_name' -c 'collection_name' --type csv --headerline --file filepath/file_name.csv

Robert Stewart's answers is great.

I'd like to add that you also can type your fields with --columHaveTypes and --fields like this :

mongoimport -d myDb -c myCollection --type csv --file myCsv.csv 
  --columnsHaveTypes --fields "label.string(),code.string(),aBoolean.boolean()"

(Careful to not have any space after the comma between your fields)

For other types, see doc here : https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-columnshavetypes


My requirement was to import the .csv (with no headline) to remote MongoDB instance. For mongoimport v3.0.7below command worked for me:

mongoimport -h <host>:<port> -u <db-user> -p <db-password>  -d <database-name> -c <collection-name> --file <csv file location> --fields <name of the columns(comma seperated) in csv> --type csv

For example:

mongoimport -h 1234.mlab.com:61486 -u arpitaggarwal -p password  -d my-database -c employees --file employees.csv --fields name,email --type csv

Below is the screenshot of how it looks like after import:

enter image description here

where name and email are the columns in the .csv file.


We need to execute the following command:

mongoimport --host=127.0.0.1 -d database_name -c collection_name --type csv --file csv_location --headerline

-d is database name

-c is collection name

--headerline If using --type csv or --type tsv, uses the first line as field names. Otherwise, mongoimport will import the first line as a distinct document.

For more information: mongoimport


Strangely no one mentioned --uri flag:

mongoimport --uri connectionString -c questions --type csv --file questions.csv --headerline 

you will most likely need to authenticate if you're working in production sort of environments. You can use something like this to authenticate against the correct database with appropriate credentials.

mongoimport -d db_name -c collection_name --type csv --file filename.csv --headerline --host hostname:portnumber --authenticationDatabase admin --username 'iamauser' --password 'pwd123'

Check that you have a blank line at the end of the file, otherwise the last line will be ignored on some versions of mongoimport


mongoimport -d test -c test --type csv --file SampleCSVFile_119kb.csv --headerline

check collection data:-

_x000D_
_x000D_
var collections = db.getCollectionNames();_x000D_
_x000D_
for(var i = 0; i< collections.length; i++)_x000D_
{    _x000D_
   print('Collection: ' + collections[i]);_x000D_
   // print the name of each collection_x000D_
   _x000D_
   db.getCollection(collections[i]).find().forEach(printjson);_x000D_
   _x000D_
   //and then print the json of each of its elements_x000D_
}
_x000D_
_x000D_
_x000D_


Just use this after executing mongoimport

It will return number of objects imported

use db
db.collectionname.find().count()

will return the number of objects.


1]We can save xsl as .csv file
2] Got to MongoDB bin pathon cmd - > cd D:\Arkay\soft\MongoDB\bin
3] Run below command
> mongoimport.exe -d dbname -c collectionname --type csv --file "D:\Arkay\test.csv" --headerline
4] Verify on Mongo side using below coomand.
>db.collectioname.find().pretty().limit(1)

Robert Stewart have already answered for how to import with mongoimport.

I am suggesting easy way to import CSV elegantly with 3T MongoChef Tool (3.2+ version). Might help someone in future.

  1. You just need to select collection
  2. Select file to import
  3. You can also unselect data which is going to import. Also many options are there.
  4. Collection imported

See how to import video


C:\wamp\mongodb\bin>mongoexport --db proj_mmm --collection offerings --csv --fieldFile offerings_fields.txt --out offerings.csv


I use this on mongoimport shell

mongoimport --db db_name --collection collection_name --type csv --file C:\\Your_file_path\target_file.csv --headerline

type can choose csv/tsv/json But only csv/tsv can use --headerline

You can read more on the offical doc.


First you should come out of the mongo shell and then execute the mongoimport command like this:

Manojs-MacBook-Air:bin Aditya$ mongoimport -d marketdata -c minibars 
--type csv 
--headerline
--file '/Users/Aditya/Downloads/mstf.csv'

2017-05-13T20:00:41.989+0800    connected to: localhost
2017-05-13T20:00:44.123+0800    imported 97609 documents
Manojs-MacBook-Air:bin Aditya$

Sharing for future readers:

In our case, we needed to add the host parameter to make it work

mongoimport -h mongodb://someMongoDBhostUrl:somePORTrunningMongoDB/someDB -d someDB -c someCollection -u someUserName -p somePassword --file someCSVFile.csv --type csv --headerline --host=127.0.0.1

Make sure to copy the .csv file to /usr/local/bin or whatever folder your mondodb is in


I was perplexed with a similar problem where mongoimport did not give me an error but would report importing 0 records. I had saved my file that didn't work using the OSX Excel for Mac 2011 version using the default "Save as.." "xls as csv" without specifying "Windows Comma Separated(.csv)" format specifically. After researching this site and trying the "Save As again using "Windows Comma Separated (.csv)" format, mongoimport worked fine. I think mongoimport expects a newline character on each line and the default Mac Excel 2011 csv export didn't provide that character at the end of each line.


All these answers above are great. And the way to go on a full featured application.

But if you want to prototype fast, want flexibility as the collection still changes as well as to minimize your early code base, there is a much simpler way that is not much discussed.

You can basically forego mongoimport by now. I could have saved 3 hours if it was mentioned here on this question. So let me share for others:

Mongodb has a GUI called Mongo Compass has both csv and json import features out of the box in a matter of clicks. It is an official part of the Mongo ecosytem. At the time of writing it is free and it works very well for my use case. https://www.mongodb.com/products/compass

  1. You simply get MongoDB compass running on your machine by following the simple installation. A couple of fields for DB connection and authentication directly in the GUI.
  2. Import the csv/json file. It took less than a second on a 30KB file to be parsed before user (me) validates.
  3. Validate the "type" of each property. Great feature, I could directly mention the property types such as booleans, integers, etc. In my experience, they seem all default to string. You can update before importing. Dates were more finicky and needed special attention on the coding side.
  4. One click further the csv is a collection in your mongo db local or on the cloud. Voila!

Given .csv file I have which has only one column with no Header, below command worked for me:

mongoimport -h <mongodb-host>:<mongodb-port> -u <username> -p <password> -d <mongodb-database-name> -c <collection-name> --file file.csv --fields <field-name> --type csv

where field-name refers to the Header name of the column in .csv file.


For the 3.4 version, please use the following syntax:

mongoimport -u "username" -p "password" -d "test" -c "collections" --type csv --file myCsv.csv --headerline

After 3 days, I finally made it on my own. Thanks to all the users who supported me.


Examples related to database

Implement specialization in ER diagram phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' cannot be loaded Room - Schema export directory is not provided to the annotation processor so we cannot export the schema SQL Query Where Date = Today Minus 7 Days MySQL Error: : 'Access denied for user 'root'@'localhost' SQL Server date format yyyymmdd How to create a foreign key in phpmyadmin WooCommerce: Finding the products in database TypeError: tuple indices must be integers, not str

Examples related to mongodb

Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Failed to start mongod.service: Unit mongod.service not found db.collection is not a function when using MongoClient v3.0 MongoError: connect ECONNREFUSED 127.0.0.1:27017 MongoDB: How To Delete All Records Of A Collection in MongoDB Shell? How to resolve Nodejs: Error: ENOENT: no such file or directory How to create a DB for MongoDB container on start up?

Examples related to csv

Pandas: ValueError: cannot convert float NaN to integer Export result set on Dbeaver to CSV Convert txt to csv python script How to import an Excel file into SQL Server? "CSV file does not exist" for a filename with embedded quotes Save Dataframe to csv directly to s3 Python Data-frame Object has no Attribute (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape How to write to a CSV line by line? How to check encoding of a CSV file

Examples related to import

Import functions from another js file. Javascript The difference between "require(x)" and "import x" pytest cannot import module while python can How to import an Excel file into SQL Server? When should I use curly braces for ES6 import? How to import a JSON file in ECMAScript 6? Python: Importing urllib.quote importing external ".txt" file in python beyond top level package error in relative import Reading tab-delimited file with Pandas - works on Windows, but not on Mac

Examples related to mongoimport

Mongoimport of json file How to use mongoimport to import csv