Errors when uploading geospatial data (polygons)

I’ve been having an incredibly frustrating time uploading polygons to a collection with a 2dsphere index. I have been constantly running into Can't extract geo keys errors on seemingly ‘clean’ polygons and I just can’t understand why. The example below shows one:

'Can't extract geo keys: { _id: ObjectId('63e519a25d8e3392b33c997e'), centroid: { type: "Point", coordinates: [ -1.20585, 50.800568 ] }, geometry: { type: "Polygon", coordinates: [ [ [ -1.208399, 50.800335 ], [ -1.208383, 50.800265 ], [ -1.208363, 50.800228 ], [ -1.208298, 50.800104 ], [ -1.208203, 50.799997 ], [ -1.208116, 50.799885 ], [ -1.20796, 50.799773 ], [ -1.207641, 50.799532 ], [ -1.207576, 50.799503 ], [ -1.207356, 50.799427 ], [ -1.207213, 50.799396 ], [ -1.207309, 50.801346 ], [ -1.207433, 50.801312 ], [ -1.207613, 50.801243 ], [ -1.207635, 50.801235 ], [ -1.208173, 50.800931 ], [ -1.20826, 50.800845 ], [ -1.208309, 50.800783 ], [ -1.208331, 50.800747 ], [ -1.208349, 50.800718 ], [ -1.208362, 50.800685 ], [ -1.208372, 50.80066 ], [ -1.208396, 50.800504 ], [ -1.208399, 50.800335 ] ] Edges 34 and 36 cross. Edge locations in degrees: [50.7999570, -1.2033620]-[50.7999550, -1.2033580] and [50.7999560, -1.2033600]-[50.8001420, -1.2031880]'

I have also uploaded the feature geoJSON to a Github Gist here. The marker you can see is one of the coordinates given in the ‘edge locations’ even though there isn’t even an edge over there!

Apart from helping me understand this error, is there a way I can flag these errors before I try to bulk upload them to our database? It’s very time consuming to create and clean these polygons everytime and only to realise there is a problem when uploading.

Hi @Ben_Said and welcome to the MongoDB community forum!!

Generally, the above error occurs when the coordinates are not properly defined while inserting the document into the collection.

However, for further understanding, could you help with the details of the deployment like:

  1. The MongoDB version you are on
  2. The index definition for the coordinates fields.

Based on the above example shared, I tried to insert the document in my local environment with version 6.0.3.

I tried to create 2dsphere index using the below command:

test> db.sample.createIndex( { ‘location.geometry’: ‘2dsphere’})
location.geometry_2dsphere

and further tried to insert the example data you posted using InsertOne command and the data was inserted successfully.

There is no direct support from MongoDB for the above ask. But the other third party links like GeoJSON Viewer and Validator could be helpful for your case.

Please note that, the above linked tool is not from MongoDB, therefore we cannot guarantee the tool’s correct validation for the GeoJSON in all cases.

Let us know if you have any further questions.

Best Regards
Aasawari