pymongo update_one not updating based on _id

Viewed 1087

I have this change set:

change_set = {'name': 'bso save job again', 'location': 'new york city', 'expires': '2020-04-04', 'created': '2020-03-05'}

with this id (i'm not creating this id, it is actually from an earlier save):

id = '5e6107ddfef5aa3c2e3647b2'

I'm trying to do this:

result = dbjobs.update_one({'_id': id}, {'$set': change_set})

my result.raw_result is this:

{'n': 0, 'nModified': 0, 'ok': 1.0, 'updatedExisting': False}

Its not throwing an error, I'm just getting zero change. Am I updating wrong?

1 Answers

TLDR: your id should be of type ObjectId and not string.

Add this to your code:

from bson.objectid import ObjectId
id = ObjectId('5e6107ddfef5aa3c2e3647b2')

Short explanation for debugging such issues:

From looking at the return value of update_one specifically the ok field returns 1, meaning the operation succeeded.

Combine that with the fact that n=0 meaning that Mongo is not able to match any documents, hence the problem is in the query section of the operation.

Related