Lopol2010
Lopol2010

Reputation: 79

mongodb import csv to json with subobjects

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.

Upvotes: 1

Views: 4109

Answers (2)

abdul rashid
abdul rashid

Reputation: 750

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

Upvotes: 1

Vivek
Vivek

Reputation: 66

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

Upvotes: 1

Related Questions