【问题标题】:Import JSON into Mongo with the _id as the filename of the JSON?将_id作为JSON文件名的JSON导入Mongo?
【发布时间】:2018-12-27 12:22:24
【问题描述】:

问题:

我有大量类似这种格式的 json 文件。

cat 00329f9608793cdf176032630ebc42ef.json
{
  "engine": {},
  "engine1": "not_found",
  "engine2": "not_found",
  "engine3": "not_found",
  "engine4": "not_found",
  "engine5": "not_found"
}

现在我创建了一个包含大量这种格式的 json 文件的文件夹。现在我想将每个 json 导入我创建的 mongo 集合中,当我尝试插入 json 文件时,_id 是使用随机哈希创建的,这不是预期的行为,我想要的是获取 JSON 的文件名并使用文件名创建 _id。

假设应该采用 00329f9608793cdf176032630ebc42ef.json 并且应该像 00329f9608793cdf176032630ebc42ef 一样创建 _id

我尝试了什么:

mongoimport --db dbName --collection myCollection --file 00329f9608793cdf176032630ebc42ef.json --jsonArray

需要什么?

有什么办法可以处理我的文件名应该存储为 _id 而不是任何随机哈希的情况。

【问题讨论】:

    标签: python json python-3.x mongodb pymongo


    【解决方案1】:

    将以下键值映射添加到原始 json 文件可能是正确的方向。

    "_id": {"$oid":"your_filename_here"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      相关资源
      最近更新 更多