My use case is as follows - I have a collection of documents in mongoDB which I have to send for analysis. The format of the documents are as follows -
{ _id:ObjectId(\"
As you mention there is currently no clean way to do what you want. The best approach at this time for operations like the one you need is this :
e.g. update({_id:{$in:[]}, state:"available", $isolated:1}, {$set:{readerId:, state:"processing"}}, false, true)
)Note that this even works in highly concurrent situations as a reader can never reserve documents not already reserved by another reader (note that step 2 can only reserve currently available documents, and writes are atomic). I would add a timestamp with reservation time as well if you want to be able to time out reservations (for example for scenarios where readers might crash/fail).
EDIT: More details :
All write operations can occasionally yield for pending operations if the write takes a relatively long time. This means that step 2) might not see all documents marked by step 1) unless you take the following steps :
Also see comments for discussion regarding atomicity/isolation. I incorrectly assumed multi-updates were isolated. They are not, or at least not by default.