6

The comments in this question: Shapefile to GeoJson conversion using C# raised another one: what is the best way to store a GeoJSON FeatureCollection in MongoDB? Is there any sort of convention when it comes to adding GeoJSON FeatureCollection to a MongoDB?

When I create a GeoJSON output with ogr2ogr:

ogr2ogr -f GeoJSON outshp.json c:\temp\us_counties_.shp

I get a json file and load that into MongoDB collection. The result is collection with a single document of type FeatureCollection, which contains all of the features:

enter image description here

It seems like a mongo collection and a featurecollection are redundant. Is there any sort of convention on how the Feature documents are stored? Are they typically left in their GeoJSON FeatureCollection? Or are they typically moved out of their GeoJSON FeatureCollection container once in a MongoDB?

enter image description here

Would there be any advantage in doing so?

The scope of my questions is the group of features that would be analogous to a shapefile or featureclass, not for cases were you start nesting geometries or feature json inside other documents.

4

It really depends on how you want to use the data once you have it in MongoDB. From the sounds of it, you will likely want to have a single MongoDB collection with a document for each feature.

This will allow you to use indexes and queries utilizing those indexes, most notably the 2dsphere index on the GeoJSON itself. There may be ways of doing that with a single document, but I don't think that makes much sense in this case.

Another thing to point out is that MongoDB is really only concerned with the geometry portion of the GeoJSON (for 2dsphere indexes). As you pointed out the FeatureCollection in the GeoJSON is mostly analogous to a collection in MongoDB. You could use the full hierarchy, but it's not required and may not make sense in some cases.

  • For my question, this is absolutely the case. – displayName Jun 11 '14 at 13:28
  • I haven't built spatial indexes yet, but indexes can be built on the nested objects so if you leave it as a GeoJSON featurecollection, you have to go 1-level deeper when building the index. Or is there performance increase when using indexes if the object is closer to the top-level of the document? – Jay Cummins Jun 11 '14 at 13:44
  • Finding answers to my own question, I think: stackoverflow.com/q/13395188/386619 – Jay Cummins Jun 11 '14 at 14:09
  • @JayCummins It may not effect performance, but my point is it will effect how you can query. If you do a spatial query (e.g. $geoWithin), the single featureCollection document will be returned if any of the features are within your query geometry. You'd have to manually loop through the features to figure out which one actually matched. There may be ways of making Mongo do that for you, but if that's what you are trying to accomplish you are better off just using multiple documents. – Evil Genius Jun 11 '14 at 14:33
  • I didn't realize that's what the query would return, so I definitely see why you would put features on the first level. – Jay Cummins Jun 11 '14 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.