When I try to create an index on geometry db.polygons.createIndex({"geometry":"2dsphere"})
, it stops at a certain polygon with the error code 16755. It says it Can't extract geo keys
and Duplicate vertices: 18 and 20
.
So upon further inspection, it seems like this happens when 2 nodes in a polygon are close together, or even duplicates.
I then go manually remove this node in QGIS and re-try the process, only to find there's another polygon with the same issue.
How can I fix this issue without having to repeat the entire process of fixing polygon > uploading to MongoDB > creating index? Is there a way I can find out how many polygons have this issue?
So what I did was, I first created the collection with the index and then tried inserting using mongoimport which gave me how many were inserted successfully.
To insert GeoJSON into MongoDB I did the following:
I hit a similar problem. I just needed to find the valid records in my dataset (I discarded the records with Duplicate Vertices).
I renamed the collection -
Then I added a single record from the original collection into a new collection and added a geospatial index to the collection
Then I added the valid records into the new collection.
Invalid records are simply ignored.
In your case you probably want to get the WriteResult from your insert, and look to see if it was successful. Something like
As another alternative, check out this question (I couldn't get this to work)