MongoDB Error Code 16755 - Can't extract geo keys & duplicate vertices

僤鯓⒐⒋嵵緔 提交于 2019-12-04 08:24:00
Aidan Ewen

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 -

db.myCollection.renameCollection('myCollectionBk')

Then I added a single record from the original collection into a new collection and added a geospatial index to the collection

db.myCollection.insert(db.myCollectionBk.findOne()) // recreate the collection
db.myCollection.createIndex({geometry:"2dsphere"}) // create the index (assumes the geometry on the record is valid)
db.myCollection.remove({}) // remove the record

Then I added the valid records into the new collection.

db.myCollectionBk.find().forEach(function(x){
    db.myCollection.insert(x);
})

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

var errors = []
db.myCollectionBk.find().forEach(function(x){
    var result = db.myCollection.insert(x);
    if (result.writeError) {
        errors.push({x._id:result.writeError.errmsg});
    }
})

As another alternative, check out this question (I couldn't get this to work)

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.

> db.someNewCollection.createIndex({"geometry":"2dsphere"})

To insert GeoJSON into MongoDB I did the following:

$ jq --compact-output ".features" yourGeoJSON > output.geojson
$ mongoimport --db someDB -c someNewCollection --file "output.geojson" --jsonArray
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!