Suggestions on how to update multiple documents with limit in place

Hi,

We have a replica set on Atlas running on 4.4.

I have a collection of documents, and I need to update some of them at the same time, but with a limit on the number of documents that I want to update at once. I also need the documents that have been updated for further processing in other systems.

I went down the path of using an aggregation that would:
$match to find the right documents
$sort so they’re in the right order
$limit the number I want to process
$set the field that I want to change

  • this obviously works, but the data isn’t persisted to the db. I then added $merge pointing to the same collection, and that does update the documents the way that I wanted, but the resulting documents are now in the database rather than in the cursor.

Ideally I want them handy so that I can continue other processes that are in other systems. I thought about just quering without $merge to get the data I wanted, and then run a separate $merge to update the db, but there is a chance that the result could be different as the source data might’ve changed since the first query was performed.

I also thought about using a unique identifier during the $set operation that I could then use to query against to get the data that had changed - is that the only option?

Thanks.