2012-08-07 57 views
1

我想从以下数据获取最近的数据 - MongoDB的

> db.points.insert({name:"Skoda" , pos : { lon : 30, lat : 30 } }) 
> db.points.insert({name:"Honda" , pos : { lon : -10, lat : -20 } }) 
> db.points.insert({name:"Skode" , pos : { lon : 10, lat : -20 } }) 
> db.points.insert({name:"Skoda" , pos : { lon : 60, lat : -10 } }) 
> db.points.insert({name:"Honda" , pos : { lon : 410, lat : 20 } }) 

> db.points.ensureIndex({ loc : "2d" }) 

获得的最近数据,然后我试图

> db.points.find({"loc" : {"$within" : {"$centerSphere" : [[0 , 0],5]}}}) 

这个时候,我得到了不同的错误

error: { 
    "$err" : "Spherical MaxDistance > PI. Are you sure you are using radians?", 
    "code" : 13461 

然后我试了

> db.points.find({"loc" : {"$within" : {"$centerSphere" : [[10 , 10],2]}}}) 
error: { 
    "$err" : "Spherical distance would require wrapping, which isn't implemented yet", 
    "code" : 13462 

如何完成此操作?我只想根据来自GEO点的给定辐射得到最接近的数据。

谢谢

+0

你找到一个解决方案@syv? – Burak 2016-01-19 06:56:45

回答

2

有几件事要注意。首先,您将坐标存储在名为“pos”的字段中,但是您在名为“loc”的字段上执行查询(并创建了索引)。

$ centerSphere将采用一组坐标和一个以弧度表示的值。所以$ centerSphere:[[10,10],2]在一个圆圈中搜索[10,10],这个圆圈是2 *(地球半径)= 12,756公里的圆。 $ centerSphere运算符不是为了在这个大的区域搜索文档而设计的(并且绕着地球两极缠绕很棘手)。尝试使用较小的值,例如.005。

最后,将坐标作为元素存储在数组中可能是一个更好的主意,因为某些驱动程序可能无法保留文档中字段的顺序(并将纬度和经度交换为完全不同的地理位置)。

+0

感谢您的意见!我得到了它的工作。 – syv 2012-08-07 18:40:02

0

希望这有助于:

地球的半径大约为3963.192英里或6378.137公里。

对于1个一:

db.places.find({ loc: { $centerSphere: [ [ -74, 40.74 ] , 
            1/3963.192 ] } })