使用mongodb 2dsphere唯一索引重复键错误 [英] Duplicate key error with mongodb 2dsphere unique index

查看:115
本文介绍了使用mongodb 2dsphere唯一索引重复键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



一个简单的重现演示:

 >版本()
2.4.5
>使用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重复键错误索引: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重复键错误索引:geo.test。$ loc_2dsphere dup key :{:1f22000102222113}
> db.test.insert({loc:{type:Point,coordinates:[113.373,23.046]}})
E11000重复密钥错误索引:geo.test。$ loc_2dsphere dup key :{:1f22000102222113}
> db.test.insert({loc:{type:Point,coordinates:[113.373,23.047]}})
E11000重复密钥错误索引:geo.test。$ loc_2dsphere dup key :{:1f22000102222113}
> db.test.insert({loc:{type:Point,coordinates:[113.373,23.048]}})
E11000重复密钥错误索引: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成功。

确实可以重现这一点。对 2dsphere 使用唯一索引并不是我认为应该支持的东西。该指数的分辨率不够高,看不到你的两点不一样。我们对S2指数的实施仅使用细胞,其最小边长为500米,而您的积分距离彼此约65米。



a href =https://docs.google.com/presentation/d/1Hl4KapfAENAOf4gv-pSngKwvS_jwNVHRPZTTDzXXn6Q/view#slide=id.i0 =nofollow> https://docs.google.com/presentation/d/ 1Hl4KapfAENAOf4gv-pSngKwvS_jwNVHRPZTTDzXXn6Q / view#slide = id.i0 ,它解释了索引是如何工作的。



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


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.

解决方案

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.

这篇关于使用mongodb 2dsphere唯一索引重复键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆