【问题标题】:Duplicate key error with mongodb 2dsphere unique indexmongodb 2dsphere唯一索引的重复键错误
【发布时间】:2013-07-24 09:07:17
【问题描述】:

我尝试使用 2dsphere 唯一索引将地理点插入 mongodb,但它会引发许多重复键错误。

一个简单的重现演示:

> version()
2.4.5
> use geo
> db.test.ensureIndex( { loc : "2dsphere" }, unique=true )
> db.test.insert({"loc" : { "type" : "Point", "coordinates" : [  113.3736642,  23.04469194 ] }})
> db.test.insert({"loc" : { "type" : "Point", "coordinates" : [  113.3734775,  23.04609556 ] }})
E11000 duplicate key error index: geo.test.$loc_2dsphere  dup key: { : "1f22000102222113" }

为什么这些完全不同的点会引发重复键错误?


更新:

我尝试了其他测试,似乎与准确性有关。

> db.test.ensureIndex( { loc : "2dsphere" }, unique=true )
> db.test.insert({"loc" : { "type" : "Point", "coordinates" : [  113.373,  23.044 ] }})
> db.test.insert({"loc" : { "type" : "Point", "coordinates" : [  113.373,  23.045 ] }})
E11000 duplicate key error index: geo.test.$loc_2dsphere  dup key: { : "1f22000102222113" }
> db.test.insert({"loc" : { "type" : "Point", "coordinates" : [  113.373,  23.046 ] }})
E11000 duplicate key error index: geo.test.$loc_2dsphere  dup key: { : "1f22000102222113" }
> db.test.insert({"loc" : { "type" : "Point", "coordinates" : [  113.373,  23.047 ] }})
E11000 duplicate key error index: geo.test.$loc_2dsphere  dup key: { : "1f22000102222113" }
> db.test.insert({"loc" : { "type" : "Point", "coordinates" : [  113.373,  23.048 ] }})
E11000 duplicate key error index: geo.test.$loc_2dsphere  dup key: { : "1f22000102222113" }
> db.test.insert({"loc" : { "type" : "Point", "coordinates" : [  113.373,  23.049 ] }})

在这个测试中 23.045 到 23.048 失败了,只有 23.044 23.049 成功了。

【问题讨论】:

  • 让我试试这个 - 这是哪个版本的 MongoDB?
  • @Derick 版本已添加到问题中。

标签: mongodb indexing geometry geospatial


【解决方案1】:

我确实可以重现这个。我认为不应该支持为2dsphere 使用唯一索引。索引的分辨率不够高,看你的两个点不一样。我们对 S2 索引的实现仅使用最小边为 500m 的“单元”,并且您的点彼此之间的距离约为 65 米。

https://docs.google.com/presentation/d/1Hl4KapfAENAOf4gv-pSngKwvS_jwNVHRPZTTDzXXn6Q/view#slide=id.i0 有一个引人入胜的演示文稿,解释了索引的工作原理。

不过,目前我认为您的问题尚无解决方案,但我会进行更多调查。

【讨论】:

  • 我的应用程序中遇到了同样的问题。从您的回答中学习后,我将 2dsphere 更改为接受重复,并在同一字段上添加了唯一的非地理空间索引。你认为它会给我关于 geoWithin 查询的问题吗?谢谢你们!
猜你喜欢
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2022-07-26
  • 2020-09-29
  • 2014-07-18
  • 2014-04-16
相关资源
最近更新 更多