How to use mongoimport to import csv

how to import csv file in mongodb in windows
import csv file into mongodb using python
how to import csv file in mongodb in ubuntu
robo 3t import csv
mongodb compass import csv
mongoimport is not defined
import csv file to mongodb using node js
import csv to mongodb atlas

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?

Your example worked for me with MongoDB 1.6.3 and 1.7.3. Example below was for 1.7.3. Are you using an older version of MongoDB?

$ cat > locations.csv
Name,Address,City,State,ZIP
Jane Doe,123 Main St,Whereverville,CA,90210
John Doe,555 Broadway Ave,New York,NY,10010
 ctrl-d
$ mongoimport -d mydb -c things --type csv --file locations.csv --headerline
connected to: 127.0.0.1
imported 3 objects
$ mongo
MongoDB shell version: 1.7.3
connecting to: test
> use mydb
switched to db mydb
> db.things.find()
{ "_id" : ObjectId("4d32a36ed63d057130c08fca"), "Name" : "Jane Doe", "Address" : "123 Main St", "City" : "Whereverville", "State" : "CA", "ZIP" : 90210 }
{ "_id" : ObjectId("4d32a36ed63d057130c08fcb"), "Name" : "John Doe", "Address" : "555 Broadway Ave", "City" : "New York", "State" : "NY", "ZIP" : 10010 }

How to use mongoimport to import csv, Your example worked for me with MongoDB 1.6.3 and 1.7.3. Example below was for 1.7.3. Are you using an older version of MongoDB? 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: Let’s take a look at the command and then break down the options afterward:

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.

How to Import a CSV into MongoDB, How do I import a CSV file into MongoDB RoboMongo? Returns information on the options and use of mongoimport.--verbose, -v¶ Increases the amount of internal reporting returned on standard output or in log files. Increase the verbosity with the -v form by including the option multiple times, (e.g. -vvvvv.)--quiet¶ Runs mongoimport in a quiet mode that attempts to limit the amount of output.

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

Import Data into MongoDB, How do I import a CSV file into MongoDB using Python? 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.

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'

How to Import a CSV into a Collection using the MongoDB Shell , Note: Use mongoimport –help to get more info about the command. 1. $ mongoimport --collection people --file people.csv --  For other use cases like importing into an existing database, importing into an existing collection, or for import from other data file types besides csv, consult the MongoDB documentation. You can alse use the bash command to see all the options available. * mongoimport –help

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

MongoDB, mongodb documentation: mongoimport with CSV. to import this data-set to the database named "test" and collection named "sample" --headerline : use the first line of the csv file as the fields for the json document. output : Let’s just go through the steps to import a CSV file. Importing a CSV file in MongoDB – steps and commands. Importing a CSV file in MongoDB is done through some commands which are followed step by step. Follow these simple steps to import the CSV: Step 1. Go to the directory where MongoDB is kept. Step 2. Go to its Bin folder. Step 3. Save or keep your CSV or TXT file here which you want to import.

How to Import CSV files in MongoDB Database, Have you ever tried MongoDB CSV Import? if not then Import CSV data into your MongoDB Database by the mongoimport utility. of the CSV file, below which listed data is mentioned (here in this example it is 'word '). As stated earlier, mongoimport works on CSV, TSV, and JSON documents. By default the import format is JSON. With the --type flag we can import CSV or TSV files. Since CSV and TSV files can contain some special features, let’s look at some of the options for working with them and mongoimport. Many times a CSV or TSV file will include a header line.

How to Import CSV, JSON, BSON & SQL to MongoDB, Use the Export Wizard instead to export MongoDB to CSV, JSON, BSON/​mongodump, SQL, and to another collection. The Import Wizard. Studio 3T's Import  mongoimport is a binary that runs from OS shell and not the mongo shell. So . C:\User\Home>mongoimport -d dbname -c collectionname --type csv --file filename.csv --headerline is correct while. mongo> mongoimport -d dbname -c collectionname --type csv --file filename.csv --headerline is not.

HOW TO CSV FILE IMPORT IN MONGODB ON WINDOWS, Create a CSV file mongodb . A CSV is a comma separated values file which allows data to be Duration: 7:29 Posted: Dec 10, 2017 By following the way you can use mongoimport to import . csv $ cat > locations.csv . Name,Address,City,State,ZIP . Jane Doe,123 Main St,Whereverville,CA,90210 . John

Comments
  • did you try use mydb before db.things.find()?
  • Yes - still shows the other document that was in db.things
  • for importing a CSV into a collection of nested documents (with sub-documents or arrays) you can use AWK - joyofdata.de/blog/import-csv-into-mongodb-with-awk-json
  • Thanks! This works for me. I was doing the same thing before with the exception of creating the CSV the way you did. Tried that and it worked appropriately. I created the original file in MS Excel, so guessing there was a problem with newline at the end of each line.
  • Worked for me too. I saved the file with LF line ending on OS X and it worked.
  • Is it possible to import all the records from the csv into just 1 object instead of 1 object per record?
  • Yes, stay away from Word when doing CSV stuff. I had the same issue. Bugs in Word and in mongoimport too :(
  • To be able to authenticate while importing you can do mongoimport -d db_name -c collection_name --type csv --file filename.csv --headerline --host hostname:portnumber --authenticationDatabase admin --username 'iamauser' --password 'pwd123'
  • Excel is indeed a weird beast when processing CSV files, that also includes trying to import CSVs on other databases, it is not strictly a problem related with mongo. I would suggest to use Google Sheets when playing around with CSVs, it's free, online and allows to import/export properly formatted CSVs and TSVs too
  • $ mongoimport -d marketdata -c minibars --type csv --headerline --file '/Users/Aditya/Downloads/mstf.csv' Gurantaed Result experienced by Manoj Barik
  • there is typo, --headerline,