Duplicate key error with mongodb 2dsphere unique index

懵懂的女人 提交于 2020-01-03 01:50:14

问题


I try to inserts geo points to mongodb with 2dsphere unique index, but it raises many duplicate key error.

A simple reproduce demo:

> 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" }

Why these totally different points raise duplicate key error?


Update:

I tried other tests, it seems to have something to do with accuracy.

> 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 ] }})

In this test 23.045 to 23.048 failed, only 23.044 23.049 succeed.


回答1:


I can indeed reproduce this. Using a unique index for 2dsphere is not something that I think that ought to be supported. The resolution of the index is not high enough to see that your two points are not the same. Our implementation of the S2 index only uses "cells" with as minimum side 500m and your points are about 65 meters away from each other.

There is a fascinating presentation at https://docs.google.com/presentation/d/1Hl4KapfAENAOf4gv-pSngKwvS_jwNVHRPZTTDzXXn6Q/view#slide=id.i0 that explains how the index works.

For now however, I don't think there is a solution to your problem but I'll do some more investigation.



来源:https://stackoverflow.com/questions/17829641/duplicate-key-error-with-mongodb-2dsphere-unique-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!