PyMongo TypeError - 文档必须是 dict、bson.son.SON 或其他继承自 collections.MutableMapping 的类型的实例

PyMongo TypeError - document must be an instance of dict, bson.son.SON, or other type that inherits from collections.MutableMapping

当我尝试将一大块 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] on new_points 变量解决了问题。