通过python / pymongo向MongoDB添加GEO2D索引会反复失败

时间:2012-03-06 16:21:55

标签: mongodb pymongo

我试图删除“指南”表并重建它并确保“geo”字段的索引但它仍然无效。以下是pymongo的信息和错误。还有为什么确保索引始终返回“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.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

1 个答案:

答案 0 :(得分:2)

似乎没有正确创建GEO索引。

运行db.guides.index_information()命令时,返回的索引为'geo_1',表示这不是geospacial索引。

有一个如何使用pyMongo创建地理位置索引的示例: http://api.mongodb.org/python/2.0.1/examples/geo.html

在pyMongo中,应该像这样创建2d索引:

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

希望这可以解决您的问题。