Search code examples
mongodbcsvimportmongoimport

How to ignore a single double quotes when importing a csv file in MongoDB?


I am trying to import a csv file to MongoDB in cmd using mongoimport.

Some of my csv fields contain a single "double quotes" like so:

Dave, 25, 406-836-3336, "51 Ashleigh St, 20141123

I would like them either to be ignored, or imported as empty string. I don't care for the address field really. I don't care how it will be imported as no operations will be made on it.

All I really care is that all the rows will be imported.


Solution

  • Replace a double-quote with double double-quote

    Dave, 25, 406-836-3336, ""51 Ashleigh St, 20141123
    

    From mongoimport docs:

    The csv parser accepts that data that complies with RFC RFC 4180. As a result, backslashes are not a valid escape character. If you use double-quotes to enclose fields in the CSV data, you must escape internal double-quote marks by prepending another double-quote.

    replace with sed

    sed 's/"/""/' test.csv > test2.csv