2013-07-24 35 views
1

我尝试用2dsphere唯一索引插入地理点到mongodb,但它引发了许多重复键错误。与mongodb 2dsphere独特索引重复的关键错误

一个简单的重现演示:

> 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成功。

+0

让我试试这个 - 哪个版本的MongoDB是这样的? – Derick

+0

@Derick版本添加到问题。 – lxyu

回答

3

我确实可以重现这一点。使用2dsphere的唯一索引不是我认为应该支持的内容。该指数的分辨率不够高,看不到你的两点不一样。我们对S2指数的实施仅使用“单元”,最小侧500米,你的点距离彼此约65米。

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

但是现在我不认为你的问题有解决方案,但我会做更多的调查。

+0

我在我的应用程序中遇到了同样的问题。在从您的答案中学习之后,我将2dsphere更改为接受dups,并在同一字段中添加了独特的非地理空间索引。你认为这会给我与geoWithin查询问题?谢谢你们俩! – lsborg

相关问题