Weird results with CSV import

In CSV import, on the 1st line, if i remove the column types, it imports just fine. Else it creates weird documents.

Example1: when CSV file has firstName.string(),lastName.string(),email.string() and 1 row of data:
Resulting Document is

    "_id" : ObjectId("630f28b72e83ac2a6aa5a0ec"),
    "firstName" : {
        "string()" : "John"
    "lastName" : {
        "string()" : "Doe"
    "email" : {
        "string()" : ""

If I remove the column types, and do the CSV import, then resulting document is

    "_id" : ObjectId("630f29402e83ac2a6aa5a0fb"),
    "firstName" : "John",
    "lastName" : "Doe",
    "email" : ""

The thing is i don’t care about the string type but for many other imports, i want to specify, boolean, timestamp, date, etc.

Has this changed in recent versions of mongo? This used to work earlier. I couldn’t find coumentation on this, hence asking


Welcome to the MongoDB Community @VenkatMSN !

What does mongoimport --version report and what command line parameters are you using?

You can specify field types with --columnsHaveTypes and would also need --headerline if the source of types is the first row in your CSV file.

I created a test CSV with:


… and imported using:

mongoimport --columnsHaveTypes --headerline test.csv --type csv -d foo -c bar

The resulting document is:

  _id: ObjectId("6310098a14219fbdb46a04c5"),
  firstName: 'John',
  lastName: 'Doe',
  email: ''

I believe your command line is missing the --columnsHaveTypes parameter, so the types in the header line will be interpreted as field names (which would be embedded documents using dot notation).


1 Like


Thanks! I was importing directly on Atlas etc and when i had used the import tool on a shell, I had only /headerline and not the columns have types.

Problem Solved


1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.