Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import Geojson file to MongoDB

Tags:

Since Geojson is actual json I thought i could to use mongoimport to load data into my MongoDB database from a .geojson file.

but i'm getting the following error:

exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0 

The file is 25MB and this is a fragment of it:

{ "type": "FeatureCollection", "features": [ {     "type": "Feature",     "id": "node/2661561690",     "properties": {         "timestamp": "2014-02-08T17:58:24Z",         "version": "1",         "changeset": "20451306",         "user": "Schandlers",         "uid": "51690",         "natural": "tree",         "id": "node/2661561690"     },     "geometry": {         "type": "Point",         "coordinates": [             -66.9162255,             10.5056439         ]     } }, // ... Omitted data {     "type": "Feature",     "id": "node/2664472516",     "properties": {         "timestamp": "2014-02-10T04:27:30Z",         "version": "2",         "changeset": "20477473",         "user": "albertoq",         "uid": "527105",         "name": "Distribuidora Brithijos (Aceites)",         "shop": "car_parts",         "id": "node/2664472516"     },     "geometry": {         "type": "Point",         "coordinates": [             -66.9388903,             10.4833647         ]     } } ] } 
like image 952
OscarVGG Avatar asked Feb 26 '14 00:02

OscarVGG


2 Answers

Download jq (it's sed-like program but for JSON)

Then run:

jq --compact-output ".features" input.geojson > output.geojson

then

mongoimport --db dbname -c collectionname --file "output.geojson" --jsonArray

like image 90
ParoX Avatar answered Sep 23 '22 00:09

ParoX


Right now you have an array of features. MongoDB will consider this to be one document. Try deleting the following from the beginning of your geojson:

{ "type": "FeatureCollection", "features": [ 

Also, delete the following from the end of your geojson:

] } 

EDIT - Also, mongo expects one document per line. So make sure that your only \n is between documents! e.g.

...     },\n     {         "type": "Feature",         "id": "node/2664472516", ... 
like image 21
Adam Avatar answered Sep 24 '22 00:09

Adam