How does MongoDb display collection data

Using Mongoimport to import CSV


Trying to import a CSV with contact information:

When you do this, no documents appear to be added to the database:

Trace says, but starting the Mongo shell and running it doesn't show any new documents.

What am i missing




Reply:


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?







I was confused about a similar issue where mongoimport gave me no error but reported importing 0 records. I had saved my file, which did not work with OSX Excel for Mac 2011 version, using the standard "Save As ..." "xls as csv" format without specifically specifying the "Windows Comma Separated (.csv)" format . After researching this site and trying the "Save As" format with "Windows Comma Separated (.csv)" again, mongoimport worked fine, I think mongoimport expects a newline character on every line, and Mac's standard CSV export Excel 2011 did not provide these characters at the end of each line.



We need to run the following command:

-d is the database name

-c is the name of the collection

--headerline If you use --type csv or --type tsv, the first line is used as the field name. Otherwise, mongoimport imports the first line as a separate document.

For more information: Mongo import


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


Make sure there is a blank line at the end of the file. Otherwise the last line is ignored in some versions of mongoimport


I use this on Mongoimport Shell

Type can choose csv / tsv / json, but only csv / tsv can use

You can read more about the official document.


Robert Stewart already answered how to import with mongoimport.

I suggest an easy way to import CSV elegantly using 3T MongoChef Tool (version 3.2+). Might help someone in the future.

  1. All you have to do is select the collection
  2. Select the file to import
  3. You can also deselect the data to import. There are also many options.
  4. Collection imported

See how to import videos


First you should get out of the shell and then run the command like this:




Use the following syntax for version 3.4:

After 3 days I finally made it on my own. Thank you to all of the users who supported me.



An error message was displayed when trying to import the CSV file. What have I done. First, I changed the header column names to uppercase and removed "-" and added "_" if necessary. Then enter the following command to import CSV to Mongo



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


Just use this after running mongoimport

The number of imported objects is returned

returns the number of objects.


use:


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

Check collection data: -



Oddly enough, nobody mentioned the flag:


Share for future readers:

In our case we had to add the parameter to make it work


My requirement was to import that into a remote instance. Worked for me for the following command:

For example:

Below is the screenshot of what it looks like after importing:

where and are the columns in the file.


At the specified file that only has one column without a header , the following command worked for me:

There field name refers to the header name the column in the file.


Make sure to copy the CSV file to / usr / local / bin or the folder where your Mondodb is located

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.