【发布时间】: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 成功了!谢谢:)