mongodb MongoDB查找和删除——最快的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8539575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB find and remove - the fastest way
提问by PetersCodeProblems
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:
我有一个简单的问题,从 mongo 集合中获取和删除对象的快速方法是什么。这是代码,我目前有:
$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.
正如您在上面看到的那样,它从数据库中抓取一个文档并将其删除(因此不会再次处理它)。无论这可能有多快,我都需要它执行得更快。挑战在于我们有多个进程执行此操作并处理他们发现的内容,但有时两个或多个进程会抓取同一个文档,因此会产生重复。基本上我需要让它这样一个文件只能被抓取一次。所以任何想法将不胜感激。
采纳答案by mpobrien
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.
彼得,在不了解所有上下文的情况下,很难说这里的最佳解决方案是什么 - 但您可以使用的一种方法是findAndModify
. 这将查询单个文档并返回它,并对其应用更新。
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/
这里有一个可能有用的例子:http: //docs.mongodb.org/manual/reference/command/findAndModify/
回答by Antoine Desbois
Use the findAndRemove function as documented here: http://api.mongodb.org/java/current/com/mongodb/DBCollection.html
使用此处记录的 findAndRemove 函数:http://api.mongodb.org/java/current/com/mongodb/DBCollection.html
The findAndRemove function retrieve and object from the mongo database and delete it in a single (atomic) operation.
findAndRemove 函数从 mongo 数据库中检索和删除对象,并在单个(原子)操作中将其删除。
findAndRemove(query, sort[, options], callback)
findAndRemove(查询,排序[,选项],回调)
- The query object is used to retrieve the object from the database (see collection.find())
- The sort parameter is used to sort the results (in case many where found)
- 查询对象用于从数据库中检索对象(参见 collection.find())
- sort 参数用于对结果进行排序(如果发现很多)
回答by David Mabodo
I make a new answer to remark the fact:
我做了一个新的回答来评论这个事实:
As commented by @peterscodeproblemsin the accepted answer. The native way to this in mongodb right now is to use the
正如@ peterscodeproblems在接受的答案中所评论的那样。现在在 mongodb 中的本地方法是使用
findAndModify(query=<document>, remove=True)
As pointed out by the documentation.
正如文档所指出的那样。
As it is native, and atomic, I expect this to be the faster way to do this.
由于它是原生的和原子的,我希望这是更快的方法。
回答by daydreamer
I am new to mongodb
and not entirely sure what your query is trying to do, but here is how I would do it
我是新手mongodb
,并不完全确定您的查询要做什么,但这是我的方法
# suppose database is staging
# suppose collection is data
use staging
db.data.remove(<your_query_criteria>)
where is a map and can contain any search criteria you want
哪里是地图,可以包含您想要的任何搜索条件
Not sure if this would help you.
不确定这是否对你有帮助。