Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSV with semicolon separator into MongoDB database

Tags:

csv

mongodb

I've just try to import a CSV file (with semicolon ; separator) into a MongoDB database. I managed import with mongoimport -d mydb -c things --type csv --file files.csv --headerline but the result is not what I'm expecting. The files have that form:

And I get the following result:

But I want to have something like:

{

   "_id": ObjectId("57b6e2654bf4a357b679305"),

   "geom_x_y" : "48.87792844925 , 2.3664591564",

   "circonfere" : "25.0",

   "adresse" : "PARIS 10E ARRDT - QUAI DE JEMMAPES",

   "hauteurnm" : "5.0",

   "espece" : "Acer platanoides",

   "varieteouc" : "'Schwedleri'",

   "dateplanta" : "31/12/2014"

}
like image 367
IRKT7 Avatar asked Aug 19 '16 17:08

IRKT7


People also ask

How do I import a CSV file into MongoDB?

You can use the mongoimport command to import CSV files into a collection using the headerline option. Headerline option notifies the mongoimport command of the first line; to be not imported as a document since it contains field names instead of data.

Can CSV use semicolon?

Adding "sep=;" or "sep=," to the CSV When you have a CSV that is separated by semicolons (;) and your system/Excel default is commas (,), you can add a single line to tell Excel what delimiter to use when opening the file. To do this: Open your CSV using a text editor.

Can I use CSV in MongoDB?

CSV (and TSV) data is tabular, and each row will be imported into MongoDB as a separate document. This means that these formats cannot support hierarchical data in the same way as a MongoDB document can.


1 Answers

mongoimport unfortunately doesn't allow you to specify your separator character. But it does work automatically with tabs as well as commas. If you know you won't ever have tabs in your input, you could replace all semicolons with tabs and that should then import correctly.

tr ";" "\t" < file.csv | mongoimport --type tsv ...
like image 79
Alex Howansky Avatar answered Nov 16 '22 03:11

Alex Howansky