Home > Software design >  How to import GeoJson to MongoDB
How to import GeoJson to MongoDB

Time:06-21

I have a GeoJson file with around 100 lines like this one:

{ "type": "Feature", "properties": { "GERK_PID": 215683, "KMG_MID": 100307201, "RABA_ID": 1100, "DOMACE_IME": "BOGOVIČ I", "DRZAVA": "S", "AREA": 2589.2056, "PERIM": 253.9002, "ST_VTX_OUT": 5, "Y_MIN": 544926.07, "X_MIN": 86435.46, "Y_MAX": 544954.47, "X_MAX": 86537.09, "Y_C": 544940.27, "X_C": 86486.275, "IND_OBLIKE": "N", "Z_AVG": 146.344, "EXP_AVG": 2, "FI_AVG": 18.0, "NAGIB_AVG": 5, "D_OD": null, "POV_HGO": 0.0, "POV_DO": 0.0, "POV_PO": 0.0, "NUP_AREA": 2589, "BLOK_ID": 113396 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 15.57945, 45.91628 ], [ 15.57915, 45.91629 ], [ 15.57917, 45.9172 ], [ 15.57953, 45.91719 ], [ 15.57945, 45.91628 ] ] ] ] } }

I would like too import data to my MongoDb used for my API. I tried to get data into table with http://geojson.io. I need the coordinates because, I will use this data for drawing polygons on the map, but I can't get them into the table.

So what procedure should I take to get all the data from GeoJson to my MongoDB?

This is link to my example file: https://mega.nz/file/oXgQCTSA#GHz7VXsWCL8bNFc0KycOMuNPHX4243E58IRRvzAmL8s

Thanks for your help!

CodePudding user response:

Assuming the schema of the features array in your example.geojson is fine for you, you could use mongoimport.

Here's a "redacted" command that creates a new database/collection on MongoDB Atlas with your file.

$ jq -c '.features[]' ./example.geojson | mongoimport --drop -d test -c geojson --maintainInsertionOrder 'mongodb srv://<loginName>:<password>@<project>.w426d.mongodb.net/?retryWrites=true&w=majority'
2022-06-19T01:39:00.415 0200    connected to: mongodb srv://[**REDACTED**]@<project>.w426d.mongodb.net/?retryWrites=true&w=majority
2022-06-19T01:39:00.440 0200    dropping: test.geojson
2022-06-19T01:39:00.653 0200    59 document(s) imported successfully. 0 document(s) failed to import.
  • Related