Search code examples
jsonmongodbcsvmongoimportsubobject

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.


Solution

  • 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