I have a collection that multiple clients (consumers) can read from every few seconds.
Every client that runs the query grab some documents from the collection based on some conditions I’ve implemented with the aggregation framework.
I need to make 100% sure the documents that the current client grabbed, will not be grabbed by any other clients.
I had two thoughts:
use $merge at the end of the aggregation on the same collection and update the state of the documents to ‘processing’ or something like that (my aggregation pipeline will skip documents with ‘processing’ state) - this feature is only available on 4.4 (latest) and a bit risky because the $merge is the last stage on the pipeline and while the pipeline runs, another client can run the same aggregation in parallel…
use ‘update’ with the logic to update only the documents that reply to my conditions and call another time to ‘find’ the documents I’ve just updated.
What do you think is the better/safer approach?
Do you have another idea of how to do it?