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"
}
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.
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.
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.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With