mongodb-执行批量查询 [英] mongodb - perform batch query
问题描述
我首先需要从集合b中查询数据,然后根据这些数据从集合b中查询数据.如:
I need query data from collection a first, then according to those data, query from collection b. Such as:
For each id queried from a
query data from b where "_id" == id
在SQL中,这可以通过联接表a& b一次选择.但是在mongodb中,它需要执行多次查询,效率似乎低下,不是吗?还是可以仅通过2个查询来完成?(一个用于a,另一个用于b,而不是1加n)我知道NoSQL不支持联接,但是有一种方法可以将for循环中的查询批量执行到单个查询中. ?
In SQL, this can be done by join table a & b in a single select. But in mongodb, it needs do multi query, it seems inefficient, doesn't it? Or it can be done by just 2 queries?(one for a, another for b, rather than 1 plus n) I know NoSQL doesn't support join, but is there a way to batch execute queries in for loop into a single query?
推荐答案
您需要分两个步骤进行操作.
You'll need to do it as two steps.
查看$in
运算符(参考),例如,允许传递_id
的数组.许多人建议您批量执行1000次.
Look into the $in
operator (reference) which allows passing an array of _id
s for example. Many would suggest you do those in batches of, say, 1000 _id
s.
db.myCollection.find({ _id : { $in : [ 1, 2, 3, 4] }})
这篇关于mongodb-执行批量查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!