Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymongo: findandmodify - "no such command" is returned

I believe there is a bug in pymongo (or, at least, the documentation) which makes it impossible to run a findandupdate query.

Here's what happens. When I run:

    result = db.command({
        'findandmodify': 'my_collection',
        'query': {'foo': 'bar'},
        'update': {'$set': {'status': 'queued'}},
    })

The query that actually gets sent to the server is:

{ 'query': {'foo': 'bar'}, 'findandmodify': 'my_collection', … }

Note that the query argument is first, and findandmodify is second.

But this causes the server to throw up:

OperationFailure: command { 'query': {'foo': 'bar'}, 'findandmodify': 'my_collection', … } failed: no such cmd

Because the server expects findandmodify to be first (BSON dicts are, apparently, ordered).

Is there any work around for this?

like image 578
David Wolever Avatar asked Dec 18 '22 02:12

David Wolever


1 Answers

For languages that don't have a built-in sorted dict type the mongo drivers include one. In python that is the SON type: http://api.mongodb.org/python/1.4%2B/api/pymongo/son.html. You will need to use that for all commands.

If that still fails make sure you are using the latest version of the database as findandmodify is a new feature.

like image 133
mstearn Avatar answered Dec 19 '22 16:12

mstearn