MongoDB 错误代码 16755 - 无法提取地理键和重复顶点

问题描述 投票:0回答:3

当我尝试在几何体

db.polygons.createIndex({"geometry":"2dsphere"})
上创建索引时,它停在某个多边形处,错误代码为16755。它说它是
Can't extract geo keys
Duplicate vertices: 18 and 20

因此,经过进一步检查,似乎当多边形中的 2 个节点靠近甚至重复时就会发生这种情况。

然后我手动删除 QGIS 中的这个节点并重试该过程,却发现还有另一个具有相同问题的多边形。

如何解决这个问题,而不必重复修复多边形>上传到MongoDB>创建索引的整个过程?有没有办法可以找出有多少多边形存在此问题?

mongodb geometry 2dsphere
3个回答
6
投票

我遇到了类似的问题。我只需要在数据集中找到有效记录(我丢弃了具有重复顶点的记录)。

我重新命名了该集合 -

db.myCollection.renameCollection('myCollectionBk')

然后我将原始集合中的单个记录添加到新集合中,并向该集合添加地理空间索引

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

然后我将有效记录添加到新集合中。

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

无效记录将被忽略。

在您的情况下,您可能想从 insert 获取 WriteResult,并查看它是否成功。类似的东西

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

作为另一种选择,请查看这个问题(我无法让它工作)


1
投票

所以我所做的是,我首先使用索引创建集合,然后尝试使用 mongoimport 插入,这给了我成功插入的数量。

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

要将 GeoJSON 插入 MongoDB,我执行了以下操作:

$ jq --compact-output ".features" yourGeoJSON > output.geojson
$ mongoimport --db someDB -c someNewCollection --file "output.geojson" --jsonArray

0
投票

我也遇到同样的问题, 快速修复:只需删除您创建的用于存储坐标的索引,然后重新创建。它将再次以空状态创建, 原因:发生此错误是因为它以某种方式在其索引中获取了重复值并且无法解析。

© www.soinside.com 2019 - 2024. All rights reserved.