MongoDB find and remove - the fastest way

前端 未结 4 902
忘掉有多难
忘掉有多难 2021-02-07 13:56

I have a quick question, what is the fast way to grab and delete an object from a mongo collection. Here is the code, I have currently:

$cursor = $coll->find(         


        
4条回答
  •  离开以前
    2021-02-07 14:11

    Peter, It's hard to say what the best solution is here without understanding all the context - but one approach which you could use is findAndModify. This will query for a single document and return it, and also apply an update to it.

    You could use this to find a document to process and simultaneously modify a "status" field to mark it as being processed, so that other workers can recognize it as such and ignore it.

    There is an example here that may be useful: http://docs.mongodb.org/manual/reference/command/findAndModify/

提交回复
热议问题