Accessing PyMongo UpdateOne operation properties

Question:

If I am receiving a list of prepared PyMongo UpdateOne operations (e.g. below)…

print(type(to_load[0]))
> <class 'pymongo.operations.UpdateOne'>

print(to_load[0])
> UpdateOne({'id': XXX}, {'$set': {'name': 'YYY'}}, True, None, None, None)

…is it possible to then extract information from these? For instance, if I want to get a list of all of the affected 'id' values [XXX, ...], is there something like the below (which does not work) that will work?

for record in to_load:
  print(record['filter'])
Asked By: OJT

||

Answers:

You can access it with the _filter attribute, with the usual caveat that _ prefixed attributes are protected and subject to change without notice.

for record in to_load:
    print(record._filter)

Reference: examining the source code.

Answered By: Belly Buster
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.