Hualiang Li
Hualiang Li

Reputation: 65

update not working with pymongo

Firstly, I find a document and update value state to 1 in DB with findandmodify() method. Then I use the document to do something. After that, I want to update the document's state to 2 with its object id, and the method I use is update().
However, after such operation, the state value didn't change to 2. Here is a brief version of my codes:

try:
    print 'update...'
    db.Tuser.update({'_id':obj_id},  # obj_id is a bson object, it's the object id of the document
                    {'$set':{'state': 2}})
    print 'update done'
except Exception as e:
    print 'update failed: %s' % str(e)

The output is:
update...
update done

By the way, this happens when I run it on 8 PCs, and only 2 of them can update successfully. While when I run it singly, it works well.

Upvotes: 0

Views: 3594

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59601

Try some basic debugging steps - Does this return anything?

db.Tuser.find({'_id':obj_id},
                {'$set':{'state': 2}})

Is obj_id a string or a bson object?

type(obj_id)

If it's a string you will need to convert it to a bson object

import bson
obj_id = bson.ObjectId(obj_id)

Upvotes: 1

Related Questions