update() never reports failure?

7 views
Skip to first unread message

Jim Jones

unread,
Apr 12, 2009, 8:56:20 PM4/12/09
to mongod...@googlegroups.com
Hi,

I'm trying to implement the "Update if Current" pattern from
http://www.mongodb.org/display/DOCS/Atomic+Operations

But it seems mongo (HEAD) is never reporting a failed update.
Please see the attached test-case.

Also this is the response that I see on the wire for any update:

T +0.000254 127.0.0.1:27017 -> 127.0.0.1:41211 [AP]
53 00 00 00 c4 4c 65 81 0b 00 00 00 01 00 00 00 S....Le.........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
01 00 00 00 2f 00 00 00 0a 65 72 72 00 08 75 70 ..../....err..up
64 61 74 65 64 45 78 69 73 74 69 6e 67 00 00 10 datedExisting...
6e 00 00 00 00 00 01 6f 6b 00 00 00 00 00 00 00 n......ok.......
f0 3f 00 .?.


cheers
-jj

pymongo_update.py

Eliot

unread,
Apr 12, 2009, 9:01:15 PM4/12/09
to mongod...@googlegroups.com
write operations don't send a response by default.
If you want to check the status, you need to call getLastError()

Some drivers have built in support.
In python for example, you can add safe=True to most write operations to make them report an error.

getLastError will also return the number of objects modified by an operation.

Jim Jones

unread,
Apr 12, 2009, 9:03:39 PM4/12/09
to mongod...@googlegroups.com
Eliot wrote:
> write operations don't send a response by default.
> If you want to check the status, you need to call getLastError()
>
> Some drivers have built in support.
> In python for example, you can add safe=True to most write operations
> to make them report an error.

I tried all of that, nothing works.
Also see the test-case, it uses both safe=True and
Connection.db.error() but neither reports a failure.


cheers
-jj

Eliot

unread,
Apr 12, 2009, 9:12:14 PM4/12/09
to mongod...@googlegroups.com
Ah - Slight misunderstanding of what should happen here.

There is no "error" since everything is valid, the update just gets applied to 0 objects.
error() only raises on error on a real error case.

getLastError tells you this:

This is the output of my modified version: (attached)
e...@erh-tnt.local ~/Downloads -> python pymongo_update.py
gle: SON([(u'updatedExisting', True), (u'ok', 1.0), (u'err', None), (u'n', 1)])
None
gle: SON([(u'updatedExisting', False), (u'ok', 1.0), (u'err', None), (u'n', 1)])
None
--DUMP--
SON([(u'i', 2), (u'_id', ObjectId('M\xbc\xe5n\xa5\x935\xe7\xad\xee+\xf0'))])
pymongo_update_eliot.py

Michael Dirolf

unread,
Apr 12, 2009, 9:21:55 PM4/12/09
to mongod...@googlegroups.com
yes as eliot points out, db.error discards some of the information
that the getLastError command gives to the driver. i'd like to reserve
db.error for the real "error" cases, but maybe we could expose another
method to get at the update info - would that be helpful?
> <pymongo_update_eliot.py>


Jim Jones

unread,
Apr 12, 2009, 9:46:41 PM4/12/09
to mongod...@googlegroups.com
Thanks to both of you for the clarification.
The error reporting works for me now.

Ideally I think the underlying getLastError() should be renamed to
getLastStatus() - because that's what it really does. Then the driver
APIs could also grow a getLastStatus() (in addition to the existing
error()) and everything would be consistent again.


cheers
-jj

Michael Dirolf

unread,
Apr 12, 2009, 9:58:14 PM4/12/09
to mongod...@googlegroups.com
if you don't mind making a feature request on jira.mongodb.org that'd
be great :)

Michael Dirolf

unread,
Apr 13, 2009, 2:03:39 PM4/13/09
to mongod...@googlegroups.com
we've decided to add this in the python driver rather than change
anything in the server:
http://jira.mongodb.org/browse/PYTHON-9

there is now a last_status() method on Database instances that will
return the entire result of the getlasterror command. try it out using
the latest PyMongo from github.

so to check if an update actually happened you can do something like:

>>> status = db.last_status()
>>> if "updatedExisting" in status and status["updatedExisting"]:
>>> ...

On Apr 12, 2009, at 9:46 PM, Jim Jones wrote:

>
Reply all
Reply to author
Forward
0 new messages