【问题标题】:"TypeError: Document must be an instance of dict" while importing JSON file into MongoDB将 JSON 文件导入 MongoDB 时出现“TypeError: Document must be an instance of dict”
【发布时间】:2020-04-22 22:58:43
【问题描述】:

我想将结构如下的 JSON 文件导入 MongoDB:

{
  "a": 1,
  "aa": 1,
  "aaa": 1,
  "aah": 1,
  "aahed": 1,
  "aahing": 1,
  "aahs": 1,
  "aal": 1,
  "aalii": 1
}

但是当我运行以下代码时:

def import_dictionary(collection):
    with open(dictionary, 'r', encoding='utf-8') as f:
        file_data = json.load(f)
        document = RawBSONDocument(BSON.encode(file_data))

    collection.insert_many(document)

我收到以下错误:

TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping

我该如何解决这个问题?提前致谢!

【问题讨论】:

    标签: python json mongodb


    【解决方案1】:

    insert_many 根据documentation 获取一个文档数组。

    要么提供一个数组,要么使用插入单个文档的 insert_one。

    【讨论】:

      猜你喜欢
      • 2017-08-14
      • 2022-09-28
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 2021-11-23
      • 2021-05-12
      相关资源
      最近更新 更多