【问题标题】:mongodb import csv to json with subobjectsmongodb 将 csv 导入带有子对象的 json
【发布时间】:2017-09-20 05:59:51
【问题描述】:

我有类似这种结构的 file.csv

loremipsum; machine, metal

据我了解,成功的导入看起来像

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

我得到的最好结果

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

如果我理解正确,请告诉我如何成功导入。谢谢。

编辑:因为“标签”对象应该包含大约 16 个 url,所以告诉我应该如何正确存储它。

【问题讨论】:

    标签: json mongodb csv mongoimport subobject


    【解决方案1】:

    理想情况下,应该使用以下命令将 csv 文件导入 mongoDb(也许您使用的是相同的):

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

    我认为,您的问题在于数组类型的数据(如果我理解正确的话......!!)。

    然后,您需要先在集合中添加一个文档并将其导出为 CSV 文件。这将为您提供上述命令所期望的格式,以正确导入您的数据。然后根据导出的 CSV 文件排列您的数据。

    link Answer Here Explained it Very well

    【讨论】:

    【解决方案2】:

    我在 Excel 中有这些数据

    我想在 MongoDB 中这样

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

    我确实替换了 cars.0 cars.1 cars.2 之类的标题

    像这样

    我使用了工具mongoimport 并运行了这个命令

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

    我的 csv 是 1.csv

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2018-08-14
      • 2015-09-23
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      相关资源
      最近更新 更多