hint with nonexistent index name- “planner returned error: bad hint"

969 views
Skip to first unread message

Kristin

unread,
Jul 19, 2018, 6:50:21 PM7/19/18
to mongodb-user
Hi there, I'm using pymongo 3.6.0 on linux and issuing a query with a hint and an index name. I thought I didn't need to worry about ensuring the collection had the specified index -- according to the docs, the hint should have no effect if the index doesn't exist: https://api.mongodb.com/python/3.6.0/api/pymongo/cursor.html#pymongo.cursor.Cursor.hint 
But the cursor throws an error when trying to retrieve the data after the query is issued.


Example:

>> cursor = collection.find({'name': 'foo'}).hint('nonexistent_index_name')
>> cursor
<pymongo.cursor.Cursor>


The query returns a Cursor, but calling anything with the cursor:

>> cursor.count()

or

>> list(cursor)

Results in the error:

  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


This query returns the expected result when I use an existing index name, or use no hint:

>> cursor = collection.find({'name': 'foo'}).hint('existing_index_name')
>> list(cursor)
[{'name': 'foo'}]
>> cursor = collection.find({'name': 'foo'})
>> list(cursor)
[{'name': 'foo'}]


Am I doing something wrong?

A. Jesse Jiryu Davis

unread,
Jul 22, 2018, 10:40:01 PM7/22/18
to mongodb-user
The PyMongo documentation for Cursor.hint is wrong; the MongoDB server returns an error if you pass a hint that requires a nonexistent index. I've created a ticket to track the documentation fix:

Kristin

unread,
Jul 23, 2018, 12:53:36 PM7/23/18
to mongodb-user
Thank you. 
Reply all
Reply to author
Forward
0 new messages