Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB find and remove - the fastest way

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()->sort(array('created' => 1))->limit(1);
$obj = $cursor->getNext();
$coll->remove(array('name' => $obj['name']));

as you can see above it grabs one document from the database and deletes it (so it isn't processed again). However fast this may be, I need it to perform faster. The challenge is that we have multiple processes doing this and processing what they have found BUT sometimes two or more of the processes grab the same document therefore making duplicates. Basically I need to make it so a document can only be grabbed once. So any ideas would be much appreciated.

like image 610
PetersCodeProblems Avatar asked Dec 16 '11 20:12

PetersCodeProblems


People also ask

How do I make MongoDB search faster?

Other ways to improve MongoDB performance after identifying your major query patterns include: Storing the results of frequent sub-queries on documents to reduce read load. Making sure that you have indices on any fields you regularly query against. Looking at your logs to identify slow queries, then check your indices.

What is the fastest operation to clear an entire collection in MongoDB?

drop() will delete to whole collection (very fast) and all indexes on the collection.

Is count faster than find MongoDB?

find({}). count() more fast then collection.


1 Answers

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/

like image 95
mpobrien Avatar answered Sep 24 '22 03:09

mpobrien