我正在使用pymongo 3.6.0并发出带有提示和索引名称的查询。我以为我不必担心确保集合具有指定的索引-根据文档,如果索引不存在,则提示应该无效:https://api.mongodb.com/python/3.6.0/api/pymongo/cursor.html#pymongo.cursor.Cursor.hint 但是在发出查询后尝试检索数据时,光标会引发错误。
示例:
>> cursor = collection.find({'name': 'foo'}).hint('nonexistent_index_name')
>> cursor
<pymongo.cursor.Cursor>
查询返回一个游标,但使用游标调用任何东西:
>> cursor.count()
或
>> list(cursor)
导致错误:
File "/python-2.7/lib/python2.7/site-packages/pymongo/cursor.py", line 1176, in next
if len(self.__data) or self._refresh():
File "/python-2.7/lib/python2.7/site-packages/pymongo/cursor.py", line 1087, in _refresh
self.__send_message(q)
File "/python-2.7/lib/python2.7/site-packages/pymongo/cursor.py", line 974, in __send_message
helpers._check_command_response(first)
File "/python-2.7/lib/python2.7/site-packages/pymongo/helpers.py", line 146, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: error processing query: ns=collection_nameTree: name == "foo"
Sort: {}
Proj: {}
planner returned error: bad hint
当我使用现有索引名称或不使用任何提示时,此查询返回预期结果:
>> cursor = collection.find({'name': 'foo'}).hint('existing_index_name')
>> list(cursor)
[{'name': 'foo'}]
>> cursor = collection.find({'name': 'foo'})
>> list(cursor)
[{'name': 'foo'}]
我做错什么了吗?
答案 0 :(得分:0)
已解决-Pymongo文档错误,并且不存在的提示确实会返回错误。 修复文档的凭单:https://jira.mongodb.org/browse/PYTHON-1615