[pymongo-motor] OperationFailure on insert

495 views
Skip to first unread message

Jorge Puente Sarrín

unread,
Sep 6, 2012, 3:44:33 PM9/6/12
to mongod...@googlegroups.com
Hi, I'm following the Motor examples, but my code raise a exception when insert a document using yield operator and motor Op/Task.

Here my code:

class ContentHandler(MotorSampleHandler):

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self):
        title = self.get_argument('title')
        slug = stringhelper.get_slug(title)
        content = {'title': title,
            'content': self.get_argument('content'),
            'slug': slug,
            'date': datetime.datetime.now()}
        yield motor.Op(self.db.contents.insert, content)
        self.redirect('/%s' % slug)

This is the traceback:

    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1000, in _stack_context_handle_exception
        raise_exc_info((type, value, traceback))
      File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 256, in _nested
        yield vars
      File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 226, in wrapped
        callback(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 382, in inner
        self.set_result(key, result)
      File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 315, in set_result
        self.run()
      File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 343, in run
        yielded = self.gen.throw(*exc_info)
      File "/home/puentesarrin/projects/motor-sample/motor-sample/handlers.py", line 111, in post
        yield motor.Op(self.db.contents.insert, content)
      File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 335, in run
        next = self.yield_point.get_result()
      File "/usr/local/lib/python2.7/dist-packages/motor/__init__.py", line 1365, in get_result
        raise error
    OperationFailure: need to login

I think that it's not about instance PyMongo Connection because, all of queries (find/find_one) results is ok.

Python version: 2.7.3
Tornado version: 2.3
PyMongo version: 2.2

--
Atte.
Jorge Puente Sarrín.

Jorge Puente Sarrín

unread,
Sep 6, 2012, 3:56:57 PM9/6/12
to mongod...@googlegroups.com
I have reviewed the DB and the document is inserted correctly, but I don't understand why raise this exception.

Regards.

2012/9/6 Jorge Puente Sarrín <puente...@gmail.com>

A. Jesse Jiryu Davis

unread,
Sep 6, 2012, 5:28:50 PM9/6/12
to mongod...@googlegroups.com
In that case it seems that you are, perhaps, logged in as a read-only user, and you're mistaken that your document was correctly inserted? Try this:

import datetime
from tornado.ioloop import IOLoop
from tornado import gen
import motor

db = motor.MotorConnection().open_sync().my_database
loop = IOLoop.instance()

@gen.engine
def go():
    print 'dropping my_collection'
    yield motor.Op(db.drop_collection, 'my_collection')
    count = yield motor.Op(db.my_collection.count)
    print 'count is', count

    content = {'title': 'title',
               'content': 'content',
               'slug': 'slug',
               'date': datetime.datetime.now()}
    yield motor.Op(db.my_collection.insert, content)
    count = yield motor.Op(db.my_collection.count)
    print 'count is', count
    loop.stop()

go()
loop.start()

Replace "my_database" with the database you're using, and run this script, please. Be aware that this script drops a collection called "my_collection."

Jorge Puente Sarrín

unread,
Sep 6, 2012, 6:01:45 PM9/6/12
to mongod...@googlegroups.com
Hi Jesse,

I have tried to run your snippet and I think that the problem is Connection, because for create it, I'm using a Uri such as:

db = motor.MotorConnection('mongodb://user:pa...@xxxxx.mongolab.com:xxxxx/dbname?safe=true').open_sync().dbname

This is the traceback,

ERROR:root:Exception in callback <tornado.stack_context._StackContextWrapper object at 0x7fc5af09bd08>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 421, in _run_callback
    callback()
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 229, in wrapped
    callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 382, in inner
    self.set_result(key, result)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 315, in set_result
    self.run()
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 343, in run
    yielded = self.gen.throw(*exc_info)
  File "<stdin>", line 4, in go
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 335, in run
    next = self.yield_point.get_result()
  File "/usr/local/lib/python2.7/dist-packages/motor/__init__.py", line 1365, in get_result
    raise error
OperationFailure: db assertion failure, assertion: 'unauthorized db:my_database ns:my_database lock type:1 client:190.41.238.178', assertionCode: 10057

Does PyMongo invoke to authenticate method when a uri have a user/password? Or Does PyMongo ignored it, when uri have a db name?

2012/9/6 A. Jesse Jiryu Davis <je...@10gen.com>

--
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

A. Jesse Jiryu Davis

unread,
Sep 6, 2012, 6:08:56 PM9/6/12
to mongod...@googlegroups.com
I see. Does that work with a regular PyMongo Connection?:

db = pymongo.Connection('mongodb://user:pass@xxxxx.mongolab.com:xxxxx/dbname?safe=true').dbname
db.my_collection.insert({}, safe=True)


On Thursday, September 6, 2012 6:02:36 PM UTC-4, Jorge Puente Sarrín wrote:
Hi Jesse,

I have tried to run your snippet and I think that the problem is Connection, because for create it, I'm using a Uri such as:

db = motor.MotorConnection('mongodb://user:pass@xxxxx.mongolab.com:xxxxx/dbname?safe=true').open_sync().dbname

Jorge Puente Sarrín

unread,
Sep 6, 2012, 6:23:23 PM9/6/12
to mongod...@googlegroups.com
No, that's not working.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 306, in insert
    continue_on_error, self.__uuid_subtype), safe)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line 745, in _send_message
    rv = self.__check_response_to_last_error(response)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line 681, in __check_response_to_last_error
    helpers._check_command_response(error, self.disconnect)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 129, in _check_command_response
    raise OperationFailure(msg % response["errmsg"])
pymongo.errors.OperationFailure: need to login

Then, what's the best way for work with a MongoDB Uri and authenticate? 

def connect(user, pass):
     yield motor.Op(db.authenticate, user, pass)

Bernie Hackett

unread,
Sep 6, 2012, 7:25:55 PM9/6/12
to mongod...@googlegroups.com
Are you sure you're using the correct credentials? I can't reproduce
the problem:

>>> c = pymongo.Connection('mongodb://user:pass@localhost:27017/foo')
>>> c.foo.bar.insert({'foo': ' bar'}, safe=True)
ObjectId('50492fdb430ee6c7d3c1435f')
>>> c.foo.bar.find_one()
{u'_id': ObjectId('50492fdb430ee6c7d3c1435f'), u'foo': u' bar'}

# Calling logout then find_one here to show that we were actually authenticated.
>>> c.foo.logout()
>>> c.foo.bar.find_one()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pymongo/collection.py", line 516, in find_one
for result in self.find(spec_or_id, *args, **kwargs).limit(-1):
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"])
pymongo.errors.OperationFailure: database error: unauthorized db:foo
ns:foo.bar lock type:0 client:127.0.0.1

On Thu, Sep 6, 2012 at 3:23 PM, Jorge Puente Sarrín
<puente...@gmail.com> wrote:
> No, that's not working.
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line
> 306, in insert
> continue_on_error, self.__uuid_subtype), safe)
> File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line
> 745, in _send_message
> rv = self.__check_response_to_last_error(response)
> File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line
> 681, in __check_response_to_last_error
> helpers._check_command_response(error, self.disconnect)
> File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line
> 129, in _check_command_response
> raise OperationFailure(msg % response["errmsg"])
> pymongo.errors.OperationFailure: need to login
>
> Then, what's the best way for work with a MongoDB Uri and authenticate?
>
> def connect(user, pass):
> yield motor.Op(db.authenticate, user, pass)
>
>
>
> 2012/9/6 A. Jesse Jiryu Davis <je...@10gen.com>
>
>> I see. Does that work with a regular PyMongo Connection?:
>>
>> db =
>> pymongo.Connection('mongodb://user:pa...@xxxxx.mongolab.com:xxxxx/dbname?safe=true').dbname
>> db.my_collection.insert({}, safe=True)
>>
>>
>> On Thursday, September 6, 2012 6:02:36 PM UTC-4, Jorge Puente Sarrín
>> wrote:
>>>
>>> Hi Jesse,
>>>
>>> I have tried to run your snippet and I think that the problem is
>>> Connection, because for create it, I'm using a Uri such as:
>>>
>>> db =
>>> motor.MotorConnection('mongodb://user:pa...@xxxxx.mongolab.com:xxxxx/dbname?safe=true').open_sync().dbname

Jorge Puente Sarrín

unread,
Sep 6, 2012, 7:40:09 PM9/6/12
to mongod...@googlegroups.com
Hi Bernie/Jesse,

I'm sure, I'm using MongoDB version 2.2 on MongoLab, Can I send you a private message with URI?

Traceback:
>>> c.db.bar.insert({'foo': ' bar'}, safe=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 306, in insert
    continue_on_error, self.__uuid_subtype), safe)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line 745, in _send_message
    rv = self.__check_response_to_last_error(response)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line 681, in __check_response_to_last_error
    helpers._check_command_response(error, self.disconnect)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 129, in _check_command_response
    raise OperationFailure(msg % response["errmsg"])
pymongo.errors.OperationFailure: need to login
>>> c.db.bar.find_one()
{u'_id': ObjectId('504932f87fffaf3e21aee2da'), u'foo': u' bar'}
>>> c.db.logout()
>>> c.db.bar.find_one()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 514, in find_one
    for result in self.find(spec_or_id, *args, **kwargs).limit(-1):
  File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 749, in next
    if len(self.__data) or self._refresh():
  File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 700, in _refresh
    self.__uuid_subtype))
  File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 657, in __send_message
    self.__tz_aware)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 102, in _unpack_response
    error_object["$err"])
pymongo.errors.OperationFailure: database error: unauthorized db:db ns:db.bar lock type:0 client:190.41.238.178

Regards.

2012/9/6 Bernie Hackett <ber...@10gen.com>

Bernie Hackett

unread,
Sep 6, 2012, 7:55:38 PM9/6/12
to mongod...@googlegroups.com
Ah, I think I understand your problem then. You're hitting this issue
which is fixed in PyMongo 2.3:

https://jira.mongodb.org/browse/PYTHON-371

You may need to pull the latest source for motor to get that change.

On Thu, Sep 6, 2012 at 4:40 PM, Jorge Puente Sarrín

Jorge Puente Sarrín

unread,
Sep 6, 2012, 10:44:33 PM9/6/12
to mongod...@googlegroups.com
Bernie/Jesse thanks a lot, problem is solved.

2012/9/6 Bernie Hackett <ber...@10gen.com>

A. Jesse Jiryu Davis

unread,
Sep 7, 2012, 10:51:54 AM9/7/12
to mongod...@googlegroups.com
Thanks Bernie, I hadn't thought of that. MongoDB 2.2 slightly changed how safe writes work with authentication, and Motor didn't catch up with that change until this commit last week:

2012/9/6 Bernie Hackett <ber...@10gen.com>
>> >>> motor.MotorConnection('mongodb://user:pass@xxxxx.mongolab.com:xxxxx/dbname?safe=true').open_sync().dbname
Reply all
Reply to author
Forward
0 new messages