1

I have file.csv with similar to this structure

loremipsum; machine, metal

As i understand, succeful import will looks like

{
  text: "loremipsum",         << string
  tags: ["machine","metal"]   << object with two fields
}

The best result i get

{
  text: "loremipsum",      <<  string
  tags: "machine, metal"   <<  string
}

If i understand it correctly then please tell me how to do succeful import. Thanks.

Edit: because "tags" object should contain ~16 urls, so tell me how it should be stored correctly.

Lopol2010
  • 79
  • 2
  • 13

2 Answers2

1

Ideally, below command should be used to import csv file to mongoDb (Maybe you are using the same):

mongoimport --db users --type csv --headerline --file /filePath/fileName.csv

I think ,Your Problem is with array type of data (if I understood correctly...!!).

Then, You need to first add one document in collection and export it as CSV file. This will give you the format which is expected by above command to import your data correctly. And then Arrange your data as per exported CSV file.

link Answer Here Explained it Very well

Vivek
  • 56
  • 2
  • 10
  • Thanks you. Also im find another easier way. Snippet code is there (3 lines of code) https://stackoverflow.com/a/29501257/6071087 you have to replace names and execute it, done. – Lopol2010 Sep 22 '17 at 02:31
1

I had this data in Excel

enter image description here

I wanted like this in MongoDB

{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
}

I did replaced the headings like cars.0 cars.1 cars.2

Like this enter image description here

I used the tool mongoimport and ran this command

mongoimport.exe --uri  "mongodb+srv:/localhost/<MYDBName>" --username dbuser --collection ----drop --type csv --useArrayIndexFields --headerline --file 1.csv

here my csv is 1.csv

abdul rashid
  • 730
  • 9
  • 21