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

时间:2015-08-28 14:55:42

标签: python pymongo geojson

当我尝试将一大块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类型吗?

1 个答案:

答案 0 :(得分:2)

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