Search code examples
pythonpymongogeojson

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


When I'm trying to insert a chunk of GeoJSON into MongoDB, I received this message: TypeError - document must be an instance of dict, bson.son.SON, or other type that inherits from collections.MutableMapping .

The chunk is like this:

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"}']

And the insert call was this:

result = points.insert_many(new_points)

This dictionary was generated with GeoJson library, using this structure:

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}

Any hint to solve this? Am I generating a wrong type of geojson?


Solution

  • Using the suggestion : [json.loads(coords) for coords in new_points] on new_points variable solves the problem.