Importing GeoJSON data into MongoDB

By peterm, 15 April, 2015

Update 5/1/15 - On a different instance of Mongo, I had run the command noted below and got errors. However, this worked:

mongoimport --db mean-dev -c points --file "points.geojson" --jsonArray

 

Let me save you the hours lost in trying to figure out getting GeoJSON features into individual documents in a collection versus one document with nested features.

MongoDB 3.0.2. Valid GeoJSON file full of 809 features.

Using this code:

mongoimport --db test --type json --file ./'mobilemaps.geojson' 

I was able to create one document in the collection named 'mobilemaps'. That single document had all the features (points and descriptive data) correclty imported.

But, I wanted a collection of documents. I'm new to MongoDB, so I'm studying and thought that might work better for my needs. Could not figure out how to get a successful import. I had read about pulling out the surrounding object and array tags:

{

  "type": "FeatureCollection",

    "features": [

...

   ]

}

The cleaned file then generated errors upon import due to the comma between features. 

So, I created a file with my feature collection, stripped off the surrounding tags. 

Then I did a search and replace:

}

 },

{

replace (e.g., remove the comma and add a new line)

}

 }

{

Running mongoimport on the resulting file gave me 809 documents in the collection. 

> db.points.find({"properties.name": "Dean of Students Office"});

{ "_id" : ObjectId("552ee3ef53049e605688ea19"), "type" : "Feature", "geometry" : { "type" : "Point", "coordinates" : [ -122.0570486784, 36.99607280576 ] }, "properties" : { "name" : "Dean of Students Office", "description" : null, "Food Type" : "", "Item Type" : "Student Services", "Parking Type" : "", "Seated Capacity" : null, "Standing Capacity" : null } }

>

Tags