在mongo`中运行多个查询 [英] Running multiple queries in mongo`
问题描述
我有一个集合,想要得到一组满足一组条件的结果。我知道Mongo不允许你使用连接,所以我需要运行单独的查询,并将结果连接到一个单一的响应。
但是可以将单独查询的结果合并在一起以获得预期的输出。
有任何基本范例,我可以看到查询结果连在一起。
感谢
例如,我可以加入这两个查询,所以我得到这两个查询的结果:
coll.find({coordinates.type:Point},{coordinates:1},tailable = True,timeout = False)
和:
coll.find({place.bounding_box.type: Polygon},{place.bounding_box.coordinates:1},tailable = True,timeout = False)
解决方案在您的具体示例中,您不需要单独运行这些查询。您可以加入结果:
coll.find(
{$ or:[
{ coordinates.type:Point},
{place.bounding_box.type:Polygon}
]
},
{coordinates place.bounding_box.coordinates:1}
)
/ $ elementMatch 而不是$或
I have a collection and want to get a set of results that met a set of conditions. I understand the Mongo doesn't let you use joins so I would need to run separate queries and join the results into a single response.
But is it possible to join the results of separate queries together to get the intended output.
Are there any basic examples I could see query results joined together.
Thanks
For example could I join these two queries so I get the results of both queries:
coll.find({"coordinates.type" : "Point"},{"coordinates" :1}, tailable = True, timeout = False)
and:
coll.find({"place.bounding_box.type" : "Polygon"},{"place.bounding_box.coordinates" : 1}, tailable = True, timeout = False)
解决方案In your specific example, you do not need to run those queries separately. You can join the results like so:
coll.find( { $or : [ { "coordinates.type" : "Point" }, { "place.bounding_box.type" : "Polygon" } ] }, {"coordinates" :1, "place.bounding_box.coordinates" : 1} )
You can also use $and / $elementMatch instead of an $or
这篇关于在mongo`中运行多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!