Hi everyone,
I ran across behavior that I think is a bug in pymongo, as running update_one with the same parameters in mongosh actually modifies the matched documents.
In my database I have objects of roughly this shape:
{
'status': 'VERIFIED',
'group': 0,
...,
'assignments': [
'assignment_id': 'LONG_STRING',
'revieved': false
...
]
}
When I run the following piece of code, the bulk_api_result shows that a single document is indeed matched, but the modified_count
is always 0:
def update_assignment_statuses_from_dict(assignment_id_status_dict: dict[str, AssignmentStatus]):
if assignment_id_status_dict:
update_operations = [UpdateOne(
{'assignments': {'$exists': True}},
{'$set': {'assignments.$[assig].status': status.value}},
array_filters=[{'assig.assignment_id': assignment_id}],
upsert=True
) for assignment_id, status in assignment_id_status_dict.items()]
bulk_results = DB.get().pages.bulk_write(update_operations)
I’m aware that an existing field with the same value will not be modified, however none of my documents have the status
field set.
Running essentially the same thing in mongosh does return a modifiedCount
of 1:
db.pages_old.updateOne({'assignments': {'$exists': true}}, {'$set': {'assignments.$[assig].status': 'MANUALLY_VERIFIED'}}, {'arrayFilters': [{'assig.assignment_id': 'SOME_ID'}]})
It seems to me that the behavior is due to the usage of bulk_write, however I could be wrong and would like some insight into this problem.
I hope I’m not too tired to see a dumb typo or something… cheers!