I have a collection where documents are inserted on a regular basis and then being ingested by several processes, following a FIFO method.
To speed up the ingestion, the processes are loading several documents in a row. When a document is taken by a process, it's being removed from the collection to avoid other concurrent processes to take the same document.
I'm using MongoOperation.findAllAndRemove (Spring MongoRepository) to do that but I'm wondering if this can be considered as "tread safe". I noticed that this method is actually doing a "find" first, then "delete" all results, I'm not sure if this can be considered as an atomic operation...
Do you confirm that MongoOperation.findAllAndRemove can be safely used, compared to the method MongoOperation.findAndRemove?
Here is my current code:
public List<IncomingMessage> findIncomingMessagesByProviderOrderByDateAsc(String provider, int limit) {
Query query = new Query();
query.addCriteria(Criteria.where("providerName").is(provider));
query.with(new Sort(Sort.Direction.ASC, "date"));
query.limit(limit);
return mongoOperations.findAllAndRemove(query, IncomingMessage.class);
}