【问题标题】:PyMongo TypeError - document must be an instance of dict, bson.son.SON, or other type that inherits from collections.MutableMappingPyMongo TypeError - 文档必须是 dict、bson.son.SON 或其他继承自 collections.MutableMapping 的类型的实例
【发布时间】:2015-08-28 14:55:42
【问题描述】:

当我尝试将一段 GeoJSON 插入 MongoDB 时,我收到了这条消息:TypeError - document must be an instance of dict, bson.son.SON, or other type that inherits from collections.MutableMapping

块是这样的:

new_points = ['{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}', '{"coordinates": [-179.75, 89.75], "simulation": "aet_whe", "type": "Point"}']

插入调用是这样的:

result = points.insert_many(new_points)

这个字典是用 GeoJson 库生成的,使用这个结构:

class GenerateDocument:
    def __init__(self, x, y, simulation_variable):
        self.x = x
        self.y = y
        self.sim = simulation_variable

    @property
    def __geo_interface__(self):
        return {'type': 'Point', 'coordinates': (self.x, self.y), 'simulation': self.sim}

有什么提示可以解决这个问题吗?我是否生成了错误类型的 geojson?

【问题讨论】:

  • 对我来说看起来像是一个字符串列表。也许你应该先[json.loads(coords) for coords in new_points] 然后通过它?
  • @Two-BitAlchemist 成功了!谢谢:)

标签: python pymongo geojson


【解决方案1】:

new_points 变量上使用建议:[json.loads(coords) for coords in new_points] 可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 2021-03-03
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多