2012-03-06 89 views
1

我尝试删除“guides”表并重建它,并确保“geo”字段的索引但仍不起作用。以下是来自pymongo的信息和错误。此外,为什么确保索引始终返回“geo_1”?通过python/pymongo向MongoDB添加一个GEO2D索引失败

>>> db.guides.ensure_index('geo', pymongo.GEO2D) 
u'geo_1' 
>>> db.guides.index_information() 
{u'geo_1': {u'unique': u'2d', u'key': [(u'geo', 1)], u'v': 0}, u'_id_': {u'key': [(u'_id', 1)], u'v': 0}} 
>>> db.guides.find_one()['geo'] 
[61.21572299999999, -149.88431399999999] 
>>> db.guides.find_one({ 'geo' : { '$near' : [62.215, -139.884] }}) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.6/dist-packages/pymongo-1.10-py2.6-linux-x86_64.egg/pymongo/collection.py", line 469, in find_one 
    for result in self.find(spec_or_id, *args, **kwargs).limit(-1): 
    File "/usr/local/lib/python2.6/dist-packages/pymongo-1.10-py2.6-linux-x86_64.egg/pymongo/cursor.py", line 601, in next 
    if len(self.__data) or self._refresh(): 
    File "/usr/local/lib/python2.6/dist-packages/pymongo-1.10-py2.6-linux-x86_64.egg/pymongo/cursor.py", line 564, in _refresh 
    self.__query_spec(), self.__fields)) 
    File "/usr/local/lib/python2.6/dist-packages/pymongo-1.10-py2.6-linux-x86_64.egg/pymongo/cursor.py", line 533, in __send_message 
    self.__tz_aware) 
    File "/usr/local/lib/python2.6/dist-packages/pymongo-1.10-py2.6-linux-x86_64.egg/pymongo/helpers.py", line 100, in _unpack_response 
    error_object["$err"]) 
pymongo.errors.OperationFailure: database error: can't find special index: 2d for: { geo: { $near: [ 62.215, -139.884 ] } } 

>>> db.guides.drop_index('geo_1') 
>>> db.guides.ensure_index('geo', pymongo.GEO2D) 
u'geo_1' 
>>> db.guides.index_information() 
{u'geo_1': {u'unique': u'2d', u'key': [(u'geo', 1)], u'v': 0}, u'_id_': {u'key': [(u'_id', 1)], u'v': 0}} 
>>> db.guides.drop_index('geo_1') 
>>> db.guides.drop_index('geo') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.6/dist-packages/pymongo-1.10-py2.6-linux-x86_64.egg/pymongo/collection.py", line 757, in drop_index 
    allowable_errors=["ns not found"]) 
    File "/usr/local/lib/python2.6/dist-packages/pymongo-1.10-py2.6-linux-x86_64.egg/pymongo/database.py", line 293, in command 
    msg, allowable_errors) 
    File "/usr/local/lib/python2.6/dist-packages/pymongo-1.10-py2.6-linux-x86_64.egg/pymongo/helpers.py", line 126, in _check_command_response 
    raise OperationFailure(msg % response["errmsg"]) 
pymongo.errors.OperationFailure: command SON([('dropIndexes', u'guides'), ('index', 'geo')]) failed: index not found 
+0

这是通过python shell的权利?你有没有尝试从mongo本地shell?我不是一个Python人(因此不直接尝试一个答案),但这可能是一个语法问题与你如何调用它,尝试通过mongo shell会消除这种可能性 – 2012-03-06 17:03:34

+0

是啊,这是python shell,我会尝试mongo本地外壳。谢谢 – 2012-03-06 19:10:52

回答

2

看起来好像GEO索引没有被正确创建。

运行db.guides.index_information()命令时,返回的索引是'geo_1',表明这不是地理空间索引。

还有就是如何创建pyMongo一个地理空间索引这里一个例子: http://api.mongodb.org/python/2.0.1/examples/geo.html

在pyMongo,二维指数应该像这样创建:

>>> db.places.ensure_index([("geo", GEO2D)]) 

希望这将解决您的问题。

+0

谢谢马克!亚当说这是一个语法问题。现在解决! – 2012-03-06 19:13:51