“点不在区间[-180,180]”MongoDB出错

时间:2012-05-07 03:05:59

标签: mongodb pymongo

我使用PyMongo声明在我的集合上创建了地理空间索引:

self.geo_collection.create_index([('location',pymongo.GEO2D)],\
         min=0,max=700,name='test_name')

但是当我尝试执行以下查询时:

{'_id': ObjectId('4fa739422d38036937000000'), 'location': {'$within': {'$polygon': [[165, 10], [165, 90], [290, 10], [290, 90]]}}}

我总是得到一个

database error: point not in interval of [ -180, 180 )

有人可以帮我弄清楚发生了什么吗?感谢。

1 个答案:

答案 0 :(得分:0)

地理空间索引的工作方式与广告一样。要使用$ polygon,您必须拥有mongodb 1.9或更高版本。我在mongodb 2.0.4上运行了以下脚本,没有任何问题

import pymongo

db = pymongo.Connection().foo
db.foo.create_index([("loc","2d")], min=0, max=700, safe=True)
db.foo.insert({"loc":(180, 50)}, safe=True)
db.foo.insert({"loc":(240, 490)}, safe=True)
db.foo.insert({"loc":(240, 530)}, safe=True)
polygonA = [ [ 165, 10 ], [ 165, 90 ], [ 290, 10 ], [ 290, 90 ] ]
for doc in db.foo.find({"loc":{"$within":{'$polygon':polygonA}}}):
    print doc

具有以下结果

{u'loc': [180, 50], u'_id': ObjectId('4fa7b47c81dde04973000000')}