如何将Geojson文件导入MongoDB

问题描述:

由于Geojson是实际的json,所以我认为我可以使用mongoimport从.geojson文件将数据加载到我的MongoDB数据库中.

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

但是我遇到以下错误:

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

文件为25MB,它是其中的一部分:

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

下载 jq (类似于sed的程序,但对于JSON)

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

然后运行:

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

然后

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