我使用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'}]我做错了什么吗?
https://stackoverflow.com/questions/51430092
复制相似问题