【问题标题】:Error 16755 - Mongo can't extract geo keys错误 16755 - Mongo 无法提取地理键
【发布时间】:2017-04-17 13:58:54
【问题描述】:

我是 mongo 和 node.js 的初学者,在现有项目中,我们有一个带有 geolocs 的商店的集合。我收到以下错误(我删除了一些字段)

"code" : 16755,
"errmsg" : "Can't extract geo keys: { _id: ObjectId('566990eea9c7a38740a305a3'), 
id: 50, guid: \"NL7a09b334-7524-102d-a4ef-00163e5faa0c\", version: 0, owner: 118, 
published: 1, loc: [ -9999, -9999 ], logo: \"69db95d0-d58d-40cf-80d3-ac80b8c86af8.png\" }  
can't project geometry into spherical CRS: [ -9999, -9999 ]"

当我尝试$set 商店的一些值时。我看到值 -9999、-9999 不正确,但是当我尝试用(在 Robomongo 中)其他值替换它时,我得到了同样的错误。如何修复此错误,以便我可以编辑 loc 中的值并执行 $set

【问题讨论】:

  • 您的更新操作如何?请包括它

标签: mongodb geolocation


【解决方案1】:

您是正确的,错误是因为您在该文档中的地理坐标无效(请参阅2dsphere indexes)。

假设这是您拥有的文档:

> db.test.find()

{
  "_id": ObjectId("566990eea9c7a38740a305a3"),
  "id": 50,
  "guid": "NL7a09b334-7524-102d-a4ef-00163e5faa0c",
  "version": 0,
  "owner": 118,
  "published": 1,
  "loc": [
    -9999,
    -9999
  ],
  "logo": "69db95d0-d58d-40cf-80d3-ac80b8c86af8.png"
}

最简单的方法是使用mongo shellperform an update,通过使用ObjectId 并执行$set on the loc field。例如,通过将 loc 字段设置为有效坐标(例如 [-73.97, 40.77]):

> db.test.update({"_id": ObjectId("566990eea9c7a38740a305a3")}, {$set:{loc:[-73.97, 40.77]}})

Updated 1 existing record(s) in 1ms
WriteResult({
  "nMatched": 1,
  "nUpserted": 0,
  "nModified": 1
})

请注意,在上面的示例中,我使用了_id 字段,它保证是唯一的,因此更新命令只会更新那个确切的文档。之后,您的 2dsphere 索引创建应该会成功:

> db.test.createIndex({loc:'2dsphere'})

{
  "createdCollectionAutomatically": false,
  "numIndexesBefore": 1,
  "numIndexesAfter": 2,
  "ok": 1
}

【讨论】:

  • 感谢您的回复,但是当我运行 db.shop.update({_id: ObjectId('566990eea9c7a38740a305a3')}, {$set:{loc:[-73.97, 40.77]}}) 时,我得到了同样的错误..
  • 你使用了mongo shell吗?我在mongo shell 中测试了这个查询。如果您使用 RoboMongo 执行更新,RoboMongo 中可能存在阻止您执行更新的问题。
  • 此外,首先不应使用无效值创建索引,也不应允许插入无效值(即,如果存在 2dsphere 索引,则该文档不应该存在在loc 字段上)。如果您仍然无法编辑该文档,最好删除并重新创建 2dsphere 索引。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 2016-03-05
  • 2019-04-06
  • 2015-05-25
  • 1970-01-01
  • 2019-04-14
  • 1970-01-01
相关资源
最近更新 更多