Using mongoimport to import a CSV file is it possible to import NULLs?

Viewed 4363

I'm trying to import from CSV into mongodb 3.4 using mongoimport, and I would like for empty columns to be imported as null values for the fields.

I was under the impression from the mongoimport documentation, that if --ignoreBlanks was not specified that I would get the behavior I wanted.

--ignoreBlanks

Ignores empty fields in csv and tsv exports. If not 
specified, mongoimport creates fields without values in imported
documents.

However, when I try to load this sample data without --ignoreblanks:

field_1.string(),field_2.int32(),field_3.string()
A,5,B
C,,D
E,7,F

then I get an error on any field that is not a string.

mongoimport --collection null_test --type csv --headerline --columnsHaveTypes --file null_test.csv --verbose 

2017-08-08T16:55:42.989+0000    filesize: 67 bytes
2017-08-08T16:55:42.989+0000    using fields: field_1,field_2,field_3
2017-08-08T16:55:43.001+0000    connected to: localhost:27017
2017-08-08T16:55:43.001+0000    ns: DEV.null_test
2017-08-08T16:55:43.001+0000    connected to node type: standalone
2017-08-08T16:55:43.001+0000    using write concern: w='1', j=false, fsync=false, wtimeout=0
2017-08-08T16:55:43.001+0000    using write concern: w='1', j=false, fsync=false, wtimeout=0
2017-08-08T16:55:43.001+0000    Failed: type coercion failure in document #1 for column 'field_2', could not parse token '' to type int32
2017-08-08T16:55:43.001+0000    imported 0 documents

For fields that are strings it loads an empty string rather than a null.

What am I doing wrong here? Is it possible to load fields as NULL using mongoimport with CSV or TSV files?

For what it's worth, if I use mongoimport to import a json file with NULLs, it imports them just fine as actual NULLs.

[
    {
        "field1": "A",
        "field2": null,
        "field3": "C"
    },
    {
        "field1": 1,
        "field2": 5,
        "field3": null
    }
]
1 Answers
Related