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()" : "john@doe.com"
    }
}

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

{
    "_id" : ObjectId("630f29402e83ac2a6aa5a0fb"),
    "firstName" : "John",
    "lastName" : "Doe",
    "email" : "john@doe.com"
}

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

Thanks

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:

firstName.string(),lastName.string(),email.string()
John,Doe,john@doe.com

… 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: 'john@doe.com'
}

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).

Regards,
Stennie

1 Like

Ugggh…

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

Thanks!

1 Like

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