Updating database and getting back data with aggregate

Hi all,

I am working on a problem that requires me to update some documents in the database, but I also need to get the full list of all updated documents and their data (so, not just the count or just id-s). This could be solved simply by using two different queries, but I want to avoid that if possible because performance holds a lot of weight in this particular problem.

After some digging, I saw the aggregate function and got very close to the desired behavior, but it diverged from what I need at the last step:
with the aggregation pipeline, I can match all my documents and alter the fields I need, but if I want the data to be written to the database, I need to use the $out operator.
As far as I managed to discover with my testing, if I use the $out operator, I cannot access the data in the calling program (Python 3.8, PyMongo 3.11.4). However, if I don’t use the $out operator, I can access the data in the calling program, but it won’t be written to the database.

Does someone have a suggestion about how to solve this problem efficiently? Did I approach this problem from a wrong angle entirely?

Thank you in advance.

Hello @Nikola_Socec, welcome to the MongoDB Community forum!

See if this is useful to your issue. You can use findOneAndUpdate method (or findAndModify method) to return the updated document. Note these two methods update one document only. With MongoDB v4.2+, all update methods support Updates with Aggregation Pipeline.

Hello @Prasad_Saya,

thank you very much for your reply. Unfortunately, I cannot use findOneAndUpdate because I need to work with multiple documents at once. Also, as far as I understand, updates with aggregation pipelines still return the pymongo.results.UpdateResult object, which can only tell me the numbers of matched and modified documents, but does not give me access to the data within the documents.