mongoimport csv with headerline and datatype

10.5k views Asked by At

I'm trying to import a csv into mongodb using the following command:

mongoimport --db users --collection contacts --file data.csv --headerline

The database exists but not the collection, I want to create it and use the first row of the csv as the field names. Why am I getting error:

error validating settings: must specify --fields, --fieldFile or --headerline to import this file type

I also would like to know:

  • how to copy/import data from one collection into another (basically the syntax)
  • how datatypes from csv are handled in mongodb when imported; do I need to specify datatypes for headers or will mongodb read it from csv types?
2

There are 2 answers

2
Naman On

To solve this:

  1. Either make sure the first line of your data.csv file has field names of the data to be parsed and then execute:

    mongoimport --db users --collection contacts --type csv --headerline --file data.csv
    

Or

  1. Define the list of field names that the values of csv would be parsed in using --fields

    mongoimport --db users --collection contacts --type csv --file data.csv --fields["name","surname","etc"]
    
0
Harsh Khandelwal On

You should write command like this:

mongoimport --db users --collection contacts --type csv --file data.csv --fields "name","surname","etc"