Mongodb:$in 运算符与大量单个查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8219409/
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 : $in operator vs lot of single queries
提问by spacenick
I know MongoDB is able to handle a lot of requests/sec, but let's say I have to query a lot of documents of a collection given their _id; what sounds better: making a $in on the _id attribute with all the ids I want to get, or loop over findOne queries?
我知道 MongoDB 能够处理很多请求/秒,但是假设我必须查询给定 _id 的集合的大量文档;什么听起来更好:在 _id 属性上创建一个包含我想要获取的所有 ID 的 $in,或者循环遍历 findOne 查询?
回答by Tyler Brock
I would definitely go with using the $in query and providing a array of _ids.
我肯定会使用 $in 查询并提供一个 _id 数组。
Example:
例子:
db.collection.find({
"key": {
"$in": [
ObjectId("xxx"),
ObjectId("yyy"),
ObjectId("zzz")
]
}
})
Why?
为什么?
- If you loop, there is a certain amount of setup and teardown for each query creating and exhausting cursors which would create overhead.
- If you are not doing this on a local machine it also creates tcp/ip overhead for every request. Locally you could use domain sockets.
- There is a index on "_id" created by default and collecting a group of documents to return in a batch request should be extremely fast so there is no need to break this up into smaller queries.
- 如果循环,每个查询都会有一定量的设置和拆卸,创建和耗尽游标会产生开销。
- 如果您不在本地机器上执行此操作,它还会为每个请求创建 tcp/ip 开销。在本地,您可以使用域套接字。
- 默认情况下在“_id”上创建了一个索引,收集一组文档以在批处理请求中返回应该非常快,因此无需将其分解为较小的查询。
There's some additional documentation hereif you want to check it out.
如果您想查看,这里还有一些额外的文档。