Pymongo and hint, OperationFailure: database error: bad hint

1,342 views
Skip to first unread message

srounet

unread,
Feb 23, 2012, 8:57:11 AM2/23/12
to mongodb-user
There is an issue in the latest pymongo driver ( 2.1.1 ) and the usage
of hint.

It seems that the hint command refuse any index_name and raise an
OperationFailure.

Reading the documentation (http://api.mongodb.org/python/current/api/
pymongo/cursor.html?highlight=hint#pymongo.cursor.Cursor.hint) it
should be used like this:

>> cursor.hint([('index_name', pymongo.ASCENDING)])

but it fail and raise an Exception.

>> pymongo.errors.OperationFailure: database error: bad hint


To reproduce the problem I have posted an example here:
http://nopaste.info/a63201cae6.html.

The steps to reproduce the Exception are just simple, if you already
have a collection, just try to use hint on it with an existing
index_name and it will fail.

You can try the exact same process in the mongo interpreter and it
will just work.

Bernie Hackett

unread,
Feb 23, 2012, 10:53:26 AM2/23/12
to mongod...@googlegroups.com
PyMongo doesn't support passing an index "name" for hint. You have to
pass the index spec. This works:

dummy_collection.find().hint([('title', pymongo.ASCENDING)])

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

srounet

unread,
Feb 23, 2012, 11:17:05 AM2/23/12
to mongodb-user
Thank you, I think it can be useful to provide an example below the
description of this method as the usage differ from what we've used to
in the mongo shell.

A. Jesse Jiryu Davis

unread,
Feb 27, 2012, 10:21:33 AM2/27/12
to mongodb-user
Hi, sorry you've had this trouble. The documentation, here:

http://api.mongodb.org/python/current/api/pymongo/cursor.html?highlight=hint#pymongo.cursor.Cursor.hint

... says:

"index should be an index as passed to create_index() (e.g. [('field',
ASCENDING)]). If index is None any existing hints for this query are
cleared. The last hint applied to this cursor takes precedence over
all others."

Let me know if you can think of a way to improve the docs.

Valery

unread,
Sep 3, 2012, 7:45:52 AM9/3/12
to mongod...@googlegroups.com
Hi Bernie,

четверг, 23 февраля 2012 г., 16:53:26 UTC+1 пользователь Bernie Hackett написал:
PyMongo doesn't support passing an index "name" for hint. You have to
pass the index spec. This works:

dummy_collection.find().hint([('title', pymongo.ASCENDING)])


in my case it doesn't work either:

>>>> rs = list(my_coll.find({'my_field' : 2177}, limit=1).hint([('my_field', pymongo.ASCENDING)]))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "pymongo/cursor.py", line 778, in next
    if len(self.__data) or self._refresh():
  File "pymongo/cursor.py", line 729, in _refresh
    self.__uuid_subtype))
  File "pymongo/cursor.py", line 686, in __send_message
    self.__uuid_subtype)
  File "pymongo/helpers.py", line 104, in _unpack_response
    error_object["$err"])
OperationFailure: database error: bad hint

>>>> my_coll.index_information()
{u'_id_': {u'key': [(u'_id', 1)], u'v': 1},
 u'my_field_1': {u'key': [(u'my_field', 1)],
                         u'safe': True,
                         u'v': 0}}

>>>> pymongo.version # installed today for pypy, albeit also reproduced with cpython 2.7.1/pymongo 2.1
'2.3+'

kind regards
Valery

Bernie Hackett

unread,
Sep 3, 2012, 11:25:08 AM9/3/12
to mongod...@googlegroups.com
I've tried reproducing your problem and can't:

>>> coll.ensure_index([('my_field', pymongo.ASCENDING)], safe=True, v=0)
u'my_field_1'
>>> pprint.pprint(coll.index_information())
{u'_id_': {u'key': [(u'_id', 1)], u'v': 1},
u'my_field_1': {u'key': [(u'my_field', 1)], u'safe': True, u'v': 0}}
>>> list(coll.find({'my_field': 2177}, limit=1).hint([('my_field', pymongo.ASCENDING)]))
[{u'my_field': 2177, u'_id': ObjectId('5044c7fa27e9c51134cade12')}]
>>> pymongo.version
'2.3+'

What version of MongoDB are you using?

BTW, safe=True is not needed with ensure_index (and ends up creating a
bogus index option if you pass it). The method always creates the
index with a safe write. I would also suggest you always use version 1
indexes instead of version 0. Your ensure_index call should just be:

coll.ensure_index([('my_field', pymongo.ASCENDING)])
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
Reply all
Reply to author
Forward
0 new messages